欢迎使用CSDN-markdown编辑器

检测扇形角度

这里用了两条Debug.DrawLine来绘制两条红色的线,用来显示所在的区域。

    float distance = Vector3.Distance(target.transform.position, transform.position);

    Quaternion right = transform.rotation * Quaternion.AngleAxis(30, Vector3.up);
    Quaternion left = transform.rotation * Quaternion.AngleAxis(30, Vector3.down);

    Vector3 n = transform.position + (Vector3.forward * distance);
    Vector3 leftPoint = left * n;
    Vector3 rightPoint = right * n;

    Debug.DrawLine(transform.position, leftPoint, Color.red);
    Debug.DrawLine(transform.position, rightPoint, Color.red);

攻击检测

检测的代码还是很少的,先贴出来

    Vector3 targetDir = target.transform.position - transform.position;
    Vector3 forward = transform.forward;
    float angle = Vector3.Angle(targetDir, forward);
    if (angle < 30.0f)
    {
        //攻击代码
    }

你可能感兴趣的:(c#,编程)