0
点赞
收藏
分享

微信扫一扫

Unity获取两个物体之间的夹角

善解人意的娇娇 2022-04-13 阅读 90
unity

若有三个物体ABC,想求夹角BA与BC两条边之间的夹角,代码如下:

Vector3.Angle(B.position-A.posion,B.position-C.position);

比如要求身体朝向与面部朝向之间的夹角,可以这么写

Vector3.Angle(Body.transform.forward, Target.transform.position - Face.transform.position);
# Body是身体,Target是眼睛注视的对象,Face是面部

最后可以根据角度控制面部转向

if (angle <= MaxAngle)
{
    transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(Face.transform.forward, Target.position - Face.transform.position, RotateSpeed * Time.deltaTime, 0.0f));
}
else
{
    transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(Face.transform.forward, Body.transform.forward, RotateSpeed * Time.deltaTime, 0.0f));
}
举报

相关推荐

0 条评论