- Home /
how to limit horizontal input when jumping
Hi Everyone,
Im new to unity and coding, so this is probably a basic question! Im trying to limit my jump to the initial force and not allow the player to turn or move in the air. Im using transform.position and vector3.right and left to move my player horizontally and rigidbody.addforce to make it jump.
How would I can calculate the velocity of my character in x when the jump key is pressed so that I can apply that to the x direction? After some trial and error, mostly error, I have come to the conclusion that rigid body.velocity must ignore my vector3.right and left.
Any help appreciated!
Answer by tormentoarmagedoom · Aug 28, 2018 at 04:22 PM
Good day.
For changing only one component of a vector, you always need to do "the same". for this example lets see how to make it change X and Z but not the Y component:
You only need to change the velocity vector each frame during the jumping time, mantaining the X and the Z but specifying what Y you want.
At the begining of the jump, you store the Y value of its velocity in a float variable.
float YVelo = rigidbody.velocity.y;
Then each LateUpdate during the jump you only need to change the Y component
rigidbody.velocity = new Vector3 (rigidbody.velocity.x , YVelo , rigidbody.velocity.z);
So Y component will remain constant during all the jump.
Bye!