计算目标在自己的哪个方向,可以利用向量的点积求解。
public Transform target;
private void CheckDirection()
{
Vector3 direction = (target.position - transform.position).normalized;
//判断前后,目标在前方dot>0,目标在后方dot<0
float dot = Vector3.Dot(transform.forward, direction);
//判断左右,目标在右侧dot>0,目标在左侧dot<0
dot = Vector3.Dot(transform.right, direction);
//计算夹角,不分正负
float angle = Vector3.Angle(transform.forward, direction);
}
网友评论