Predicting max jump height of a force impulse
Hi !
I have a rigidbody2D to which I apply this jump routine :
IEnumerator JumpRoutine()
{
Vector2 jumpVector = new Vector2 (0f, jumpForce);
// === Velocity Cut ===
// source : http://gamasutra.com/blogs/DanielFineberg/20150825/244650/Designing_a_Jump_in_Unity.php
// Add force on the first frame of the jump
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpVector, ForceMode2D.Impulse);
// Wait while the character's y-velocity is positive (the character is going up)
while(isPressingJump && rigidbody2D.velocity.y > 0)
{
yield return null;
}
// [not important] If the jumpButton is released but the character's y-velocity is still positive...
if (rigidbody2D.velocity.y > 0)
{
//...set the character's y-velocity to 0;
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
}
}
Basically, it applies a force impulse of jumpForce
when the user presses Jump. As long as the user keeps the Jump key down, the rigidbody2D acts like normal (going as high as the initial force impulse allows him to). But when he releases the key, the rigidbody2D brutally stops going up (y-velocity to 0).
I tried to calculate the maximum height the rigidbody2D can reach if the user keeps the Jump key down long enough. Is there a way to simulate/predict the jump and place a mark at the rigidbody's future position ? Or can it be calculated with some physics ?
(My goal is to draw a debug line showing the max height the player can reach with a jump).
Thanks! Seeven.
Answer by Seeven · Oct 04, 2015 at 05:10 PM
For the record, I found what I needed :
Here is how I calculate the max jump height :
void CalculateJumpLine()
{
float g = rigidbody2D.gravityScale * Physics2D.gravity.magnitude;
float v0 = jumpForce / rigidbody2D.mass; // converts the jumpForce to an initial velocity
float maxJump_y = groundCheck.position.y + (v0 * v0)/(2*g);
// For Debug.DrawLine in FixedUpdate :
lineStart = new Vector3(-100, maxJump_y, 0);
lineEnd = new Vector3(100, maxJump_y, 0);
}
Hope it helps some people.
in my way i just changed 0 to little value like 3-4 to smooth bruttally stops in air :) if (GCRB.velocity.y > 0) { GCRB.velocity = new Vector2(GCRB.velocity.x,4); }
Your answer
Follow this Question
Related Questions
How to AddForce up to a certain height and then stop? 0 Answers
Instantiated Prefab no Rigidbody2D on Update function 1 Answer
my 2d character isnt jumping with constant velocity on applying force 1 Answer
rigid.velocity.x = 0 for unknow reason 0 Answers
Bullet not moving rigidbody2d,AddForce not working for bullet 1 Answer