OnTriggerExit and OnTriggerEnter not working

Viewed 64

I have a problem. I want isOffRoad to become true whenever the wheel enters a collider attached to an object with a tag offRoadBorder, and for it to become false upon exiting it. Nothing seems to work. Both collider and the wheel have rigidbody2D attached. Thanks in advance.

public bool isOffRoad = false;
void OnTriggerEnter2D(Collider2D col)
{
    Debug.Log("a");
        
    if (col.tag == "offRoadBorder")
    {
        isOffRoad = true;
    }
}
    
void OnTriggerExit2D(Collider2D col)
{
    Debug.Log("b");
        
    if (col.tag == "offRoadBorder")
    {
        isOffRoad = false;
    }
}
2 Answers

Make sure that they both have a collider and that both colliders have the "Is Trigger" enabled.

Check IsTrigger field in collider componenets. Triggers are using for physically empty colliders that have script uses.

enter image description here

Otherwise use OnCollision2D:

void OnCollisionEnter2D(Collision2D col)
{
    if (col.transform.CompareTag("offRoadBorder")) isOffRoad = true;
}
void OnCollisionExit2D(Collider2D col)
{
    if (col.transform.CompareTag("offRoadBorder")) isOffRoad = false;
}
Related