- Home /
Rigidbody2D AddForce only working with large numbers
I have a character that I am trying to make jump. I can make them move left and right with inputs and setting velocity and what not, but the jump isn't working. I'm using AddForce like so:
"Jump" appears in the console so the function is being called, but the character doesn't move. However, when I put the number being multiplied in the function (125) to something really high like 500, the character shoots up in the air. Does anyone know why it is working like this?
AddForce is based on the Rigidbody mass, heavier thing need more force, if you specify Force$$anonymous$$ode in the second parameter of the AddForce, some Force$$anonymous$$ode ignore mass value.
Yes, this indeed is the solution that @sergio7888 pointed out because by default AddForce adds force on the rigidbody's mass. In your case, I'd recommend you to use Force$$anonymous$$ode.Impulse or Force$$anonymous$$ode.Velocity in the second parameter of the AddForce method, depending on your use.
Answer by IanGRap · Oct 13, 2016 at 07:00 AM
Figured it out. I had the gravity scale set super high on the associated rigid body 2d.
Answer by Naphier · Oct 13, 2016 at 06:59 AM
A few things here:
You're applying force every fixed update frame. This means you are apply this force around 60x per second. This means by the time the character leaves the ground a lot of force has been added.
Think of reality. If you jump do you continually have a force pushing you upward? No. It's more of an instantaneous force. For this reason we most commonly use ForceMode.Impulse which applies the amount of force indicated times 1 second. So the total force that would be applied over 1 second of time is applied instantaneously. Force = mass * distance / time^2.
Don't do this with Input.GetButton, instead use Input.GetButtonDown to only register a single keypress until the key is released and pressed again.
Make sure you have your drag set up properly (usually little drag is appropriate) and make sure your mass is sufficient (usually 1 is OK). Other than that double check your gravity is set to -9.81 m/s^2.
Last one, don't ever put Debug.Log statements in an Update loop, they block the main thread and slow your application. Learn how to debug things like this with OnGUI or exposing variables in the inspector.
Your answer
Follow this Question
Related Questions
Simple Rigidbody2D movement and jump 0 Answers
2D "Platformer" Movement Problems 1 Answer
AddForce from Two Sources 1 Answer
Rigidbody.Addforce makes object skip 5 Answers