- Home /
Trouble detecting when Rigidbody2D character lands. (Not so simple)
Hello all,
I need help checking if my RigidBody2D character has landed from a jump. I can't seem to think of a way to get around these issues:
Must allow for PlatformEffector2D for one way platforming. This means we can't simply raycast below us or else when we jump through a platform from below, we will be considered 'landed' or 'grounded' in the middle of our jump.
Must allow for jumping onto slanted platforms. We also can't simply check to see if the y velocity is <= 0, because if we jump onto a slanted platform and continue running uphill, then our velocity never drops below or = 0.
Jumping right after landing must feel reactive. I mention this because I would just use OnCollisionEnter2D and check the impact point's normal, but OnCollisionEnter2D feels incredibly delayed, and seems like it's getting called late, which is kind of annoying.
I'm not really sure what other approaches I can take that don't get messy, so if anyone has any suggestions please let me know! I've been struggling with this for a while, and would like to move on.
Thanks everyone, Lawrence
For your first problem, you could just check to see if the platform has a PlatformEffector2D and wait until the velocity is
I'd recommend looking at Sebastian Lague's series here.
Thanks, I like watching Sebastian's tutorials, but the issue with his implementation is 1) He doesn't use a rigid body, which means no built in physics. All simulations would have to be custom built, and 2) he uses raycasts to detect collisions, so he would not be able to use the built in PlatformEffector2D for one way platforms.
Does anyone know if there is a way to mimic OnCollisionEnter2D without the frame delay? If I could do that then my problems would be solved.
Answer by I-am-Lawrence · Mar 18, 2017 at 10:51 AM
So I did find a way to detect when a rigidbody lands. I compared the angle of the Rigidbody's velocity to the angle of the ground below the player. If the velocity is perpendicular or tilted toward the negative normal, or there is no velocity, then we can assume the rigidbody has landed. I don't know if using raycasts in this manner will behave properly with PlatformEffector2D colliders, but so far so good...
So my formula looks something like this... hit is a downward raycast a short distance to the ground.
if (Rigidbody.velocity.sqrMagnitude == 0)
IsJumping = false;
else
{
float VelocityAngle = Vector2.Angle(-hit.normal, Rigidbody.velocity);
if (VelocityAngle <= 90)
IsJumping = false;
}
Your answer
Follow this Question
Related Questions
2D walls affect my jump height 1 Answer
Raycast2D Only returns true 1 Answer
Detect RigidBody2D Collision Only With Certain Velocity 0 Answers
player jumps too high when clicking with two fingers. 2 Answers
Simple Rigidbody2D movement and jump 0 Answers