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!

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.