Home > OS >  OnTriggerExit and OnTriggerEnter not working
OnTriggerExit and OnTriggerEnter not working

Time:05-29

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;
    }
}

CodePudding user response:

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;
}

CodePudding user response:

Do they both have a collider and do both colliders have the "Is Trigger" enabled?

  • Related