2D IsOnGround check providing false positives
This is a common question, but I have a deeper issue than most people discuss. See the attached image of a character jumping against a ledge. The blue box is the characters rigid body, currently falling from the jump. The green box is the ledge. The yellow arrows are intersections retrieved from the physics sim. My shapecast for ground detection is not shown.
The image shows two arrows. These are the positions and normals of the intersections received from the characters RigidBody hitting the ledge via the Unity sim. The character is lined up exactly with the edge of the ledge. When jumping the top of the RigidBody hits the bottom edge of the ledge and produces the bottom arrow. The character proceeds past the top of the ledge, falls and clips the top edge of the ledge, producing the top arrow.
Neither of these intersections affect the sim, to the player, they just jumped and landed as normal and the ledge did not affect their movement in any way.
This is desired results.
The problem is that my ground detection function that shapecasts down 0.02 (the ledge is 1.0 tall) from the players rigid body INTERSECTS the ledge on the way down and gets a normal facing upwards. Unity's sim gets a sideways normal, but my shapecast gets an upwards normal. The intersection point is EXACTLY the edge of the ledge. Naturally this tells me I'm on the ground, when I am not.
Floating point issues FTW, as they always are. I've been struggling with similar issues to this for years and have yet to find a proper solution. My goal is to use this shapecast function to determine if the character is on the ground. Other solutions I've seen relate to OnCollisionEnter/Exit, which has similar unreliable results.
Any ideas?
[1]: /storage/temp/126787-jump-against-wall.png
Your answer
Follow this Question
Related Questions
How to trigger a BoxCollider with a RigidBody? 0 Answers
Is there any good sample code for shooting a projectile and collision detection? 0 Answers
Moving rigidbody with addforce, velocity or position causes another object not to collide anymore. 0 Answers
Collider seems bigger than it is 0 Answers
Difference in friction between objects with Rigidbody and without 0 Answers