我有一个角色/刚体,'她'可以转身.当我在Unity中按Play时,如果我向前/向后移动,那很好,她向前/向后移动.这是一个好的开始.
但是如果我向左或向右转,然后前进/后退,她现在向侧面移动.
她是一个刚体组件,设置为场景中的父级.
当然这并不难做到,但我无法弄清楚如何设置她的旋转,以便当她转身时,当我按下按钮向前移动时,她将向前移动!有很多第一人称射击游戏,你可以转向并向前移动,玩家朝着正确的方向前进.
我的旋转脚本目前是这样的:
Vector3 EulerAngleVelocity;
public float rotateSpeed = 250.0f;
void Update() {
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow))
{
MoveVector = PoolInput();
Move();
}
if (Input.GetKey(KeyCode.RightArrow))
{
EulerAngleVelocity = new Vector3(0, rotateSpeed, 0);
Quaternion deltaRotation = Quaternion.Euler(EulerAngleVelocity * Time.deltaTime);
rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
}
}
private void Move()
{
rigidbody.AddForce((MoveVector * moveSpeed));
}
private Vector3 PoolInput()
{
Vector3 dir = Vector3.zero;
dir.x = joystick.Horizontal();
dir.z = joystick.Vertical();
if (dir.magnitude > 1)
dir.Normalize();
return dir;
}
Run Code Online (Sandbox Code Playgroud)