Best ground 2D ground check strategy? How to detect if a object is touching the top of a layer?
Heya.
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.
Does IsTouchingLayer() detect if you are touching the top of the ground?
Collider2D.IsTouchingLayers uses Layer $$anonymous$$asks, so perhaps you need to setup a layer to identify the top of the ground, then pass the required parameter when doing the check. http://docs.unity3d.com/ScriptReference/Collider2D.IsTouchingLayers.html
Otherwise, post relevant code (also perhaps some screen grabs on how the scene is set up?) to help others help you.
Answer by Socapex · Oct 29, 2015 at 11:34 AM
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.