Home > Net >  OnCollisionEnter2D(Collision collision) isn't triggering despite collision
OnCollisionEnter2D(Collision collision) isn't triggering despite collision

Time:07-19

I am new to Unity and having problems with this method. Any help would be appreciated.

Currently, I have one box gameObject (customer) that falls down onto another box gameObject (table). The script with theOnCollisionEnter2D method is attached to the tableBox object and checks using tags if the customerBox has collided with it. The problem is that despite colliding, OnCollisionEnter2D does not seem to be getting called at all, as nothing is appearing from Debug.Log. Both boxes have Rigidbody2D and a boxCollider2D.

I have tried changing the Rigidbody2D property to dynamic and also double checking the tags are correct. It is also possible that the script for the tableBox is not running at all, which if is the case I am not sure how to fix and would appreciate suggestions.

Here is the method:

void OnCollisionEnter2D(Collision2D collision){
        Debug.Log("in the method.");
        
        if(collision.gameObject.tag == "customer")
        {
          Debug.Log("Collision detected!");
        }
}

CodePudding user response:

is the "IsTrigger" checkbox unchecked on your collider component ? Check if the gameobjects are correctly configured (layers, colliders, tags, your script...) and if you got layers, check the physics settings ;)

CodePudding user response:

Your code seems to be good. Check the following

  1. Both the Rigidbodys are set to dynamic.
  2. Both the bodies have 2D colliders.
  3. They are not colliding already.
  4. Both Colliders are not set as trigger.
  5. Check the layer collision matrix.(Edit>Project settings>Physics2D)
  • Related