- Home /
How to check if character reached jump apex / peak
So, this is supposedly a simple question, but i'm having problems with this for about two weeks now...
What I need:
I just need to know when my character has reached the jump peak/apex, so I can run an animation at that point and perform other routines.Where I'm currently at:
I'm checking in the Update() method if the vertical velocity equals 0 and if the character is not on the ground, like this: if (Mathf.Approximately(myRB.velocity.y, 0.0f) && !isGrounded) // On Apex (not working on jump peak)
{
Debug.Log("apex");
}
isGrounded is a boolean variable that is currently working perfectly, which uses a trigger collider.
What I've already also tried:
Checking in the FixedUpdate() method, and not using Mathf.Approximately (just checking with myRB.velocity.y == 0), and all possible combinations with these variations.What is going on:
As you already know, myRB.velocity.y is positive when jumping, and negative when falling. In theory, with gravity as acceleration, the velocity should gradually reduce from the initial positive value (when the jump starts), until the character reaches the peak of its jump, when, in that exact moment, the velocity equals 0, and then the player starts descending with increasing (in the negative direction) velocity.After some practical tests, the function only logs "apex" when the character hits its head on a platform above him or when he falls off a platform (only sometimes)...
Tl;dr:
myRB.velocity.y does not equals 0 when the character reaches its jump peak. How can I check if he reached its jump apex via C# script?Thanks!
Answer by fafase · Mar 05, 2018 at 07:53 AM
It could be your velocity is never 0, one frame it is positive and one frame it turns negative. That frame is the highest point (actually the previous was but...).
So when the value flips, you got the moment:
bool highPt = false;
void Update()
{
if(rb.velocity.y < 0 && this.highPt == false)
{
this.highPt = true;
ProcessHighPt();
}
if(isGrounded){ this.highPt = false; }
}
When the velocity is negative and highPt is false, it will perform the ProcessHighPt. Since you flip the highPt boolean to true, it won't call next frame.
When your character is grounded again, it will set it back to false for next jump.
But you also to make sure that if you collide with something above, you should not run the animation since you did not reach the apex. So you should cancel on collision.
void OnCollisionEnter2D(Collision2D col){
CancelApex();
}
CancelApex would most likely cancel your animation. This is because the FixedUpdate is called before the Collision.
More advanced way would be to register the animation to be triggered on LateUpdate. This way, the FixedUpdate only registers but does not start, then the Collision may or may not unregister, and the LateUpdate performs if registered.
I'm amazed... It worked without a hassle! And I'll also be able to use this scriptable trigger concept with a million of other applications in my game! Thank you so much!!
Dude thank you so much for telling me if the velocity of y is flip from positive to negative that's mean it's the peak of a jump thank you so much. I just solved my problem for checking if the player is falling or not, and also solved my jump buffering feature for my platformer game. God bless you bro
Just calculate the velocity of y axis by doing transform.position.y - oldPosition and then I just check if the player is already reach the peak apex and if the velocity.y is less than zero and done, check for falling or not done
Your answer
Follow this Question
Related Questions
Can't jump in C# 1 Answer
Max height jump 2 Answers
Character not jumping high enough 1 Answer