- Home /
The object turn but the axis dont (SOLVED)
Hello,
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??
Answer by Bunny83 · Sep 18, 2017 at 11:09 AM
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 = Vector3.zero;
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;
Thank you for your answer!! It really help me in my project and for future ones too.
Really appreciate the help Bunny83!! :D
Answer by MT369MT · Sep 18, 2017 at 05:48 AM
Hi Try to use: moveInput = Vector3.forward;
Thank you for the answer $$anonymous$$T369$$anonymous$$T,
But i want to be able to controle the object(player), if i use the Vector3.forward it will always go forward.
Answer by cgarossi · Sep 18, 2017 at 11:01 AM
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.
Hello cgarossi, thanks for your answer,
but if i implement your code i won't be able to controle the player with w,a,s,d, it will always go forward.
The problem is when i rotate the player the axis don't rotate with him.
An example: if i turn my player 180 degrees the z axes didn't rotate and will have the same position, so if i press w for walk forward he will walk for the side.
Your answer
Follow this Question
Related Questions
Build rotation tool for level editor 0 Answers
Rotating Cube with oculus touch controllers 0 Answers
Smoothly rotate object based on GetAxis 1 Answer