On Collision not working for gameobject. C#/ Unity

72 Views Asked by At

enter image description here

enter image description here

IDK what I'm doing. On collision is not working does the mouth have to have ridged body? Or should I use ontrigger.

4

There are 4 best solutions below

1
Vionix On

OnCollisionEnter2D This can be used to detect collision between two colliders that are not set as Trigger and at least one of them has a Rigidbody attached to it.

Syntax

void OnCollisionEnter2D(Collision2D col)
{
    //Your code
}

OnTriggerEnter2D OnTiggerEnter2D is used to detect collision between 2D colliders that are set as a trigger. You need to use this, even if one of the Collider is set as Trigger and at least one of them needs to be a Rigidbody.

Syntax

void OnTriggerEnter2D(Collider2D col)
{
   //Your Code
}

Read this more details: basics of Unity collision

0
Ak Khan On

I think the reason behind it is: that you are not using a 2D collider on both of the objects. please have a look at it, To implement it accurately. https://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html

0
Comaneanu Mugurel On

In order for those events to be triggered you need to make sure of two things:

  1. Both of the items need to have either 2d or 3d colliders attached as components to them (but make sure they are of the same type, 2d collider won't work with a 3d collider);
  2. At least one of the items has a rigidbody component attached to them. If you don't need the rigidbody functionality you can always just check the "isKinematic" property.
0
Sudhir Kotila On

Just try this use param "Collision2D" instend of "Collider2D"

 private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.name=="fallSprite")
        {
            // Do Your Code Here...
        }
    }