Unity 精准伤害检测

 

 

using System;
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;
}
}
posted @ 2023-08-15 17:51  三页菌  阅读(72)  评论(0编辑  收藏  举报