How can I make an object jump at the same height by applying the same force?

I’m working on a 2D game, when the user hits the jump button, the following c# script executes:

void Jump(){
  if(Jumping) return;
  if(Time.realtimeSinceStartup-LastJump>0.4f){
     Anim.SetBool("Jumping",true);
     Body.AddForce(new Vector2(0,350));
     Jumping = true;
  }
}

As you can see, no jump is allowed if there is another in progress, and there must be a 0.4 seconds time span between jumps. Jumping is set to false when the jump animation ends.

Since I’m applying constant force on each jump, I’m expecting all the jumps to reach the same height, but that’s not what I’m getting. The jumping object sometimes only reaches about 75% of the expected height, and about 1 out of every 100 times doesn’t even leave the ground.

So why is this happening?.. and how can I make my object always jump at the same height by applying the same force?

To apply the force on FixedUpdate is just not enough, in order to warranty same height jumps you also need to make sure the object speed is the same… theoreticaly speed in the y axis is always cero whenever I’m applying the force, however without this line:

Body.velocity = new Vector2(0,0);

Same height jump are just not possible.

Could your script be changing a value on your player after the force is applied?

Try applying the force in a coroutine that yields to fixed update, that way you add the force once all the scripts are done messing with things but before the physics are calculated.

Are you only moving the player via physics?