The object turn but the axis dont (SOLVED)


Im making a project where i use the “w,a,s,d” to move the object and the “g,j” to turn the player in the y axis. this works but when i make a 360 degrees turn my z axis stay in the same position so instead using the w key to walk i have to use the s key.

The code i’m using:

	void FixedUpdate() {
        moveInput = new Vector3(Input.GetAxisRaw("HorizontalP1"), 0, Input.GetAxisRaw("VerticalP1"));
        //moveInput = new Vector3(Input.GetAxisRaw("HorizontalP1"), 0, 0);
        moveVelocity = moveInput * moveSpeed;
        myRigidBody.velocity = moveVelocity;
        if (Input.GetKey(rotationLeft))
            transform.Rotate(Vector3.up, -moveRotation * Time.deltaTime);

        if (Input.GetKey(rotationRight))
            transform.Rotate(Vector3.up, moveRotation * Time.deltaTime);

How can i make the axis rotate with the object??

The velocity of a rigidbody is always defined in worldspace as any physics simulation is done in world space. So the way you set your velocity is always in worldspace. There are several ways how you can solve this. The most simplest one is to first define your movement vector in local space. Your current moveInput can be treated as localspace. Then just transform this direction into worldspace by using transform.TransformDirection().

// input in local space
moveInput = new Vector3(Input.GetAxisRaw("HorizontalP1"), 0, Input.GetAxisRaw("VerticalP1"));

// transform into worldspace:
moveInput = transform.TranslateDirection(moveInput);

moveVelocity = moveInput * moveSpeed;

Another way would be to compose the final move input by directly adding and scaling the appropriate worldspace vectors together:

moveInput =;
moveInput += transform.right * Input.GetAxisRaw("HorizontalP1");
moveInput += transform.forward * Input.GetAxisRaw("VerticalP1");
moveVelocity = moveInput * moveSpeed;

Keep in mind that the way we handle the input at the moment we use a square “input field”. So moving diagonally will make you move faster than just running straight forward. That’s because the diagonal of out input square has a lenght of Sqrt(2) (~1.414…). There are other games out there which did not account for this.

One solution is to use “ClampMagnitude” to clamp the length of the input vector to “1”:

moveInput = Vector3.ClampMagnitude(moveInput, 1f);
moveVelocity = moveInput * moveSpeed;

Clamp magnitude allows the vector to be shorter but not larger than the specified length. That way no matter which direction you run you always run at the specifed max speed and not faster.

Keep in mind that setting the velocity directly will cancel any velocity that might be applied by the physics system (like gravity). You may want to preserve the y-velocity.

// ....
moveVelocity.y = myRigidBody.velocity.y;
myRigidBody.velocity = moveVelocity;

Try to use:
moveInput = Vector3.forward;

Z is forward in world space.

So, to get the local forward of your object use:

myRigidbody.velocity = transform.forward * moveSpeed;

transform.forward is your local forward axis.