I was testing jump heights for a 3D platformer game I'm making with double jump and found that the height the player achieves doing a grounded jump was slightly different than jumping in mid-air. More specifically, I considered the following player logic attached to a capsule with standard size on a cube at (0, 0, 0). Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class player : MonoBehaviour { private Rigidbody rb; public float maxHeight; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody>(); } // Update is called once per frame void Update() { if(Input.GetKeyDown(KeyCode.Space)) rb.AddForce(Vector3.up*(5-rb.velocity.y), ForceMode.Impulse); } void FixedUpdate() { maxHeight = Mathf.Max(maxHeight, transform.position.y); rb.AddForce(Vector3.down*5, ForceMode.Acceleration); } } I tracked the max height for each jump and found that the player jumped 2 units for the first jump, but the second jump was around 2.4 units, even though I am adding almost the same force with each jump. How can I make the jump heights of the two jumps exactly the same? Is this a quirk with the physics system?
You are adding force by doing: Code (CSharp): Vector3.up * (5 - rb.velocity.y) When doing that the second jump will be smaller if the player still goes upwards, and bigger if is falling. Instead you should do: Code (CSharp): Vector3.up * 5 But this will make the jump still inconsistent, so you also have to do: Code (CSharp): rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z This will reset the y velocity of the rigidbody. Also, that code allows you to make infinite jumps