Great tutorial! SUPER informative and fun at the same time. Thank you for making and sharing this with us <3
I wanted to understand the math responsible for jumping, so just in case if anyone else is interested, here's what I got so far.
I'm excluding gravity scaling for the sake of simplicity and clarity, but it can be easily plugged into the formulas.
New Gravity Calculation:
Vector2 newGravity = new Vector2(0, (-2 * jumpHeight) / (timeToJumpApex * timeToJumpApex)); body.gravityScale = (newGravity.y / Physics2D.gravity.y) * gravMultiplier;
using the formula:
distanceTravelled = 0.5 * gravity * time^2
Jump Speed calculation, based on current gravity:
jumpSpeed = Mathf.Sqrt(-2f * Physics2D.gravity.y * body.gravityScale * jumpHeight);
using formulas:
currentVelocity = gravity * time
distanceTravelled = 0.5 * gravity * time^2
In the code, the multiplier inside sqrt is -2, because the in unity gravity has a direction (down, duh...), thus it's negative. Whereas this formula considers g as magnitude, without direction, making it positive. To account for this, we multiply g by -1, and get -2 * g * d. (I hope this gibberish makes sense)