OnCollisionEnter2D and OnCollisionExit2D

For some reason I can’t figure out how to get OnCollisionEnter2D &Exit2D to work.I have attached a small hitbox to the bottom of my character in a child but it seems to not be able to check when it hits the ground and when it exits. It stays in one state and when you hit a corner it goes berzerk. My main aim is for it to not be able to double or triple jump. I have the parent script check if the variable isGrounded is true or false and jump accordingly.The parent script is called Move2D if you need it.

Any help would be greatly appreciated. :slight_smile:

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

public class Grounded : MonoBehaviour
{
    GameObject Player;
    // Start is called before the first frame update
    void Start()
    {
        Player = gameObject.transform.parent.gameObject;
    }

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

    }
    void OnCollisionEnter2D(Collision2D Collision){
        if(Collision.collider.name == "Ground_1"){
            Debug.Log("Hit with ground");
            Player.GetComponent<Move2D>().isGrounded = true;
        }
    }
    void OnCollisionExit2D(Collision2D Collision){
        if(Collision.collider.name == "Ground_1"){
            Debug.Log("Left the ground");
            Player.GetComponent<Move2D>().isGrounded=false;
        }
    }
}

If it don’t detect collision probably you don’t have any rigidbody2d. At least one of the object must have a rigidbody2d to detect the collision. For example in your case the object where “Grounded” is attached to must have a rigidbody2d componenet too or the object “Ground_1”

It sometimes detects the collision but not all the time. It is pretty strange

Do you use transform to move the object?