定义一个GameObject,用于旋转的对象。
旋转的对象最好为主角的子对象,如果旋转的对象与移动的对象在同一Transfrom,
则会导致移动的混乱,因为旋转的同时旋转了坐标的朝向。
public GameObject playerRot;
transform.forward:为对象的正方向
若未改变Y轴的轴向,则代表着Z轴
向量的叉乘:AxB=C,C垂直于A,B
AxB>0则A在B的顺时针方向
AxB<0则A在B的逆时针方向
AxB>0则A在B的顺时针方向
AxB<0则A在B的逆时针方向
void playerRotate(Vector3 target,GameObject rot)
{
//向量的叉乘
if (Vector3.Cross(rot.transform.forward, target - this.transform.position).y > 0)
{
//计算角度
rot.transform.Rotate(Vector3.up * Vector3.Angle(rot.transform.forward, target - this.transform.position));
}
else
{
rot.transform.Rotate(Vector3.down * Vector3.Angle(rot.transform.forward, target - this.transform.position));
}
}