非第一人称模式下,任务操作WASD,W人物超上冰向上走,A人物转向左并超左走
先获取到要转向的方向的角度向量(h=input,getAxis("horezontal"),v=.......)
Vector3 tagetDir = new Vector3(h, 0f,
v);
Vector3 nowDir = transform.forward;
float angle = Vector3.Angle(nowDir, tagetDir);//获取夹角
rotation函数沿着vector3.up旋转时,只能顺时针,为了避免出现需要逆时针旋转小度数顺时针旋转一个大的度数,对所有逆时针旋转式进行修正
当人物左转时,如果面朝上,就让得到的角度为负,其他类推。
//为了避免转90°变为转270°做出修正
if (Input.GetKey(KeyCode.A) &&
(transform.localRotation.eulerAngles.y < 90 ||
transform.localRotation.eulerAngles.y > 270))
{
angle = -angle;
}
else if (Input.GetKey(KeyCode.S) &&
transform.localRotation.eulerAngles.y > 180)
{
angle = -angle;
}
else if (Input.GetKey(KeyCode.D) &&
(transform.localRotation.eulerAngles.y < 270 &&
transform.localRotation.eulerAngles.y > 90))
{
angle = -angle;
}
else if (Input.GetKey(KeyCode.W) &&
transform.localRotation.eulerAngles.y < 180)
{
angle = -angle;