public void Update()
{
Debug.DrawRay(this.transform.position, this.transform.forward, Color.red);//射线
Debug.DrawLine(this.transform.position, GameObject.FindWithTag("Player").transform.position, Color.red);//线段
float a = Vector3.Angle(this.transform.forward, (GameObject.FindWithTag("Player").transform.position - this.transform.position).normalized);//得到夹角
Vector3 ab = GameObject.FindWithTag("Player").transform.position - this.transform.position;//AB之间的向量
if (a < 45 && a > 0 && ab.magnitude < 5 && ab.magnitude > 0)//限定范围
{
print("发现入侵者!");
}
}
用法:
1.创建两个球A,B,其中一个球B的标签选择:Player(方便查找)
2.将脚本挂载A上即可