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
- Both the Rigidbodys are set to dynamic.
- Both the bodies have 2D colliders.
- They are not colliding already.
- Both Colliders are not set as trigger.
- Check the layer collision matrix.(Edit>Project settings>Physics2D)