using System;标签:checkPointsOld,void,Vector3,raycastHit,public,Unity,checkPointsTrans,伤害,精准 From: https://www.cnblogs.com/sanyejun/p/17632022.html
using UnityEngine;
public class TestGameAnimEevnt : MonoBehaviour {
[Range(0,1)]
public float timeScale = 1;
public Transform[] checkPointsTrans;
public Vector3[] checkPointsOld;
public bool check;
private RaycastHit[] raycastHit = new RaycastHit[16];
void Start() {
checkPointsOld = new Vector3[checkPointsTrans.Length];
}
void FixedUpdate() {
Time.timeScale = timeScale;
if (check) {
for (int i = 0; i < checkPointsTrans.Length; i++) {
Check(checkPointsOld[i], checkPointsTrans[i].position);
Debug.DrawLine(checkPointsOld[i], checkPointsTrans[i].position, Color.red);
checkPointsOld[i] = checkPointsTrans[i].position;
}
}
}
private void Check(Vector3 startPoint, Vector3 endPoint) {
Vector3 dir = endPoint - startPoint;
var lineDistance = dir.magnitude;
Array.Clear(raycastHit, 0, raycastHit.Length);
// 射线检测
int hits = Physics.RaycastNonAlloc(startPoint, dir.normalized, raycastHit, lineDistance);//障碍物层级
for (int i = 0; i < hits; i++) {
Debug.Log(raycastHit[i].collider.gameObject.name);
}
}
// 开启检测
public void TestEventOpen() {
for (int i = 0; i < checkPointsTrans.Length; i++) {
checkPointsOld[i] = checkPointsTrans[i].position;
}
check = true;
}
// 关闭检测
public void TestEventClose() {
check = false;
}
}