判断前后左右和求角度

2019-03-27  本文已影响0人  于阿法

计算目标在自己的哪个方向,可以利用向量的点积求解。

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);
}
上一篇 下一篇

猜你喜欢

热点阅读