'Unity2D how do i calculate jump height using Velocity & Gravity

i would like to calculate the jump height. but i don't know how

Example:

RightBody2D rb = this.GetComponent<RightBody2D>();

// Do jump
rb.velocity = new Vector2(rb.velocity.x, jump);

// Here is the question. how do i calculate jump height
float jumpHeight = CalculateJumpHeight(jump); 

indicator.transform.position = new Vector2(transform.position.x, jumpHeight);


Solution 1:[1]

You can calculate this as follows, however, it's not going to be perfectly accurate in every instance because Unity's physics system isn't deterministic.

    float timeToReachApexOfJump = Mathf.Abs(rb.velocity.y / Physics.gravity.y);
    float heightOfJump = (0.5f * Physics.gravity.y * timeToReachApexOfJump) + (rb.velocity.y * timeToReachApexOfJump);
    Debug.Log(timeToReachApexOfJump);
    Debug.Log(heightOfJump);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HumanWrites