Unity2D 如何使用速度和重力计算跳跃高度



我想计算跳跃高度。但我不知道是怎么回事

示例:

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);

您可以按如下方式计算,但是,它不会在所有情况下都完全准确,因为Unity的物理系统不是确定性的。

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);

最新更新