Decided to change my character movement from transform.Translate to a rigidbody movement, the only problem is that my character is not moving to where he is facing, also it falls really slowly in the Y axis. Heres the code related to the movement:

    float moveHorizontal = Input.GetAxis("Horizontal") * rotSpeed;
    float moveVertical = Input.GetAxis("Vertical") * speed;

    rb.velocity = new Vector3(0, 0, moveVertical);
    transform.Rotate(0,moveHorizontal, 0);

1 Answer 1


Velocities are specified in the world coordinate system. So (0, 0, moveVertical) means a velocity pointing along the world's Z axis, not the character's Z axis.

If you want to specify your velocity in local coordinates, all you have to do is convert them to world coordinates before you assign the velocity, something like this:

var localVelocity = new Vector3(0, 0, moveVertical);
var worldVelocity = transform.TransformVector(localVelocity);

// Preserve jumping/falling.
worldVelocity.y = rb.velocity.y;

rb.velocity = worldVelocity;

Though for a single axis you can take a shortcut:

worldVelocity = transform.forward * moveVertical
              + Vector3.up * rb.velocity.y;

One unrelated error here is that you're changing your position using the rigidbody's velocity, but changing your rotation using the transform. Combining physics- and non-physics movement can confuse the physics engine and cause bugs, and will disable interpolation which can make your movement look jerky. So, it's better to keep all rotation under the control of the rigidbody:

// transform.Rotate(0, moveHorizontal, 0);
var rotation = Quaternion.Euler(0, moveHorizontal, 0) * rb.rotation;

You'll probably also want to scale your rotation by deltaTime to keep it smooth and consistent under varying framerates. (You don't need to do this with velocity since it's already a rate over time, integrated over the time step by the physics engine)

  • \$\begingroup\$ I just have a problem, the line of code rb.velocity = transform.forward * moveVertical; makes my falling way to slow and turns the jump into a teleportation. What might be wrong? Everything else works fantastic. \$\endgroup\$
    – Hugo Sousa
    Commented Aug 9, 2019 at 15:56
  • 1
    \$\begingroup\$ Probably because you're overwriting velocity.y with zero. Since jumping/falling wasn't in your original question, we weren't able to see that you needed that. I've edited the answer to show one way to persist the vertical velocity. \$\endgroup\$
    – DMGregory
    Commented Aug 9, 2019 at 20:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .