【发布时间】:2020-12-12 22:26:21
【问题描述】:
我正在统一制作第三人称角色控制器,我让玩家移动带有方向和动画。我的问题是:如何使运动旋转相对于相机旋转
注意: 我不使用 transform.translate 或任何类似的方法来移动播放器,我正在使用输入向量和 atan2 计算旋转,然后使用动画移动播放器。所以我不能只改变x方向和y方向,我只能改变y旋转
我当前的播放器外观代码
Vector3 CalculateRotation(float H, float V)
{
IsTurning = IsMoving(H, V);
if (IsTurning) {
angle = Mathf.RoundToInt(Mathf.Atan2(V, -H) * Mathf.Rad2Deg);
IsTurned = false;
ani.SetBool("isWalking", true);
return new Vector3(0, Mathf.RoundToInt(angle), 0);
}
else
{
ani.SetBool("isWalking", false);
}
return new Vector3(0, Mathf.RoundToInt(CurrentRotation.y), 0);
}
void LookTorwards(Vector3 T_Angle)
{
NewRotation = T_Angle - CurrentRotation;
NewRotation.y = Mathf.Repeat(NewRotation.y + 180f, 360f) - 180f;
if (NewRotation.y == 180)
{
NewRotation.y -= 1;
}
if (TargetRotation.y == CurrentRotation.y)
{
IsTurned = true;
return;
}
if (NewRotation.y > 0)
{
transform.Rotate(new Vector3(0, RotationSpeed, 0));
CurrentRotation.y += RotationSpeed;
}
else if (NewRotation.y < 0)
{
transform.Rotate(new Vector3(0, -RotationSpeed, 0));
CurrentRotation.y -= RotationSpeed;
}
}
【问题讨论】: