- Home /
Character Jump Control
I would like to know how can I control the jump movement of a character in Unity3D. Basically I am trying to program the jump in such a way that while a jump is in progress the character is allowed to move either left or right in mid-air when the corresponding keys are pressed. With my script the character correctly move to the left for example, when the left key is pressed, but if this movement is in progress and between that progression when the right key is pressed, the character moves to the right before the movement to the left is completed. In short I just need to prevent lateral ship movement while the ship is already moving laterally? Or is there any simpler way to achieve this.
Answer by NoseKills · Jun 03, 2014 at 06:19 AM
You need to move the rigidbody sideways by using forces just like you do when jumping. Then just prevent applying sideways forces when not "touchingPlatform" . This way the horizontal speed will be preserved but cant be changed while jumping.
You could also have your own variable for horizontal speed that gets modified as you press left/right on the ground, and use that to set transform.position to achieve the same behavior, but directly changing transform.position of rigidbodies is not safe anyways. You can e.g. change the position to be inside another solid object that way, and the physics engine can't check for collisions since the object just warped there