若有三个物体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));
}