Having a slight problem with trying to predict what force to apply to my character’s jump action to reach a set height.
I think my math is correct but I’ll go over it anyway.
In order to calculate the desired velocity I use this basic formula:
FinalVelocity² = InitalVelocity² - (2 * RateOfDeceletation * Distance)
I grabbed this from some random website. I assume it’s correct.
Switching stuff around, as we know the FinalVelocity will be 0 (at the apex of the jump) and given the InitalVelocity (JumpForce) and a RateOfDeceletation (Gravity) I can arrive at the stopping distance (JumpHeight) like so:
JumpHeight = JumpForce² / (2 * Gravity)
The inverse of this being
JumpForce = SquareRoot(Gravity * JumpHeight * 2)
Yet, if I put all this into unity the results from the physics simulation are usually off by 10 to 15% and it seems to vary a lot based on the physics timestep. I’ve tried using rigidbody.AddForce() with the appropriate force modes as well as adjusting the rigidbody.velocity value directly in case Physx was doing something weird with object mass and size calculations but it never runs how it should.
If I plug in a jump height of 2.0 units and calculate the correct jump force, my character jumps 2.22 units high with a physics timestep of 0.03 and 2.14 units high with a physics timestep of 0.02
I can fudge the formula to get it to work. For example:
SquareRoot(Gravity * JumpHeight * 1.75)
The above gets much closer for a physics timesetp of 0.03, but I just don’t like breaking the laws of physics and I’m worried I’ve missed a caculation somewhere.
Does anyone have any insights to share about Unity’s physx hat could explain this?