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);
}
}

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:

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”:

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.