Best ground 2D ground check strategy? How to detect if a object is touching the top of a layer?


Im currently making a basic 2D platformer. Up until i have been using the Collider2D IsTouchingLayer function but just realised that i can get away with infinite jumps while touching the bottom of platforms. How would i detect if the player is just touching the TOP of the ground?
Thanks in advance.

You can check if it is under you, but you will need OnCollissionX or OnTriggerX methods. For example (non tested, but should work).

if (coll.gameObject.transform.position.y <= transform.position.y)
    // Object colliding with is under you

In reality, you’ll want to check with (coll.gameObject.transform.position.y + coll.gameObject.renderer.bounds.extents.y) and (transform.position.y - renderer.bounds.extents.y).
This will give you the exact “bottom” of your character and “top” of platform.
Hope this helps.