I’m making a side-scroller and recently upgraded from transform.position to rigidbody.AddForce, the character controls nicely, but I’m new to code. I use rigidbody.AddForce to get him to move forward and backward, I also use it for jumping.
The problem is is that when moving right or left, forward or backward, force keeps being added and his movement get kinda out of hand. Jumping works fine, but, if I keep pressing the jump button, he keeps jumping, even in mid-air. Anything I could do? Here’s my code.
Add in a “grounded” check for the character. If the player hits the ground (that can be checked in a variety of ways), then the character becomes grounded again and can jump. If they jump, grounded is set to false.
You would be much better off using a Character Controller instead of having to rely on accurate simulated physics to move your character. As for the grounded issue, cast a ray X units downwards from the character, and if it hits something then set a boolean variable “grounded” to true. Then in the jump script check if the character is grounded before jumping.
Have you played with drag? In theory that would provide a terminal velocity.
I’ve read a common way to hack this is to provide a negative force in the opposite direction as the force increases. Did you do some google research? It should bring up UnityAnswers topics and there are quite a few.
I imagine your character have a rigidbody attached and the ground is represented by some sort of collider. If so, you can use the collision event available onOnCollisionEnter(Collision collision) and set here the variable grounded to true and set it to false in your update like this