How to detect child box collider collision with 2D tilemap

I have a player that has a GroundCheck box collider to check if the player is grounded and can jump.
It works fine, except you can jump up walls as the isGrounded code isn't specific to the GroundCheck box collider - the player can be rubbing along a wall but is still grounded. I was wondering if there was a way of modifying the code below to detect if ONLY the GroundCheck box collider is collding with the tilemap.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grounded : MonoBehaviour {
    GameObject Player;
    // The GameObject of the Player

    // Start is called before the first frame update
    void Start() {
        Player = gameObject.transform.parent.gameObject;

    // Update is called once per frame
    void Update()

    private void OnCollisionEnter2D(Collision2D collision) {
    // OnCollisionEnter2D is a special procedure name recognised by Unity
        if (collision.collider.tag == "Terrain")  {
            Player.GetComponent<Movement>().isGrounded = true;
            // Sets the isGrounded Boolean to true
    private void OnCollisionExit2D(Collision2D collision) {
    // OnCollisionExit2D is a special procedure name recognised by Unity
        if (collision.collider.tag == "Terrain") {
            Player.GetComponent<Movement>().isGrounded = false;
            // Sets the isGrounded Boolean to true

In case you want to use a collider, you can set the walls and the ground into different layers and canceling the collision between the player's layer and the layer where you put the wall in.
What might also work and be quite easier is adjusting the box collider's x value to be inside the player's sprite width, so it won't collide when hitting the walls.
Not an expert, but I would reccommend you using an OverlapBox or OverlapSphere instead of using another collider.

1 Like

You can simply query the existing contacts rather than perform an intersection test but filter them by layer, direction etc. Filtering is done by specifing the ContactFilter2D which is available for all queries.

Here's a video showing a basic "IsGrounded" controller. Info is in the video description.

This is the script: SimpleGroundedController.cs where this line gets contacts and uses filtering. If you look in the scene, it's set to only be interested in contacts in an arc from below.

1 Like

The file has been moved, working link here

I've updated the links above.