Home > database >  unity c# OnCollisionEnter2D() Not working
unity c# OnCollisionEnter2D() Not working

Time:07-20

I can't get a collision detection to work between and Enemy object and a Projectile object. I tried looking online for anyone else having similar problems, but haven't been able to find anything that relates to the problems I'm having.

I'm not getting any errors in the console or warnings.

In my c# code that's a component to and object with a Rigidbody2D and CircleCollider2D I have

void OnCollisionEnter2D(Collision2D collision)
{
    print("collision");
    if (collision.transform.tag == "Projectile") {
        print("Collision with Projectile");
    }
}

Not getting collision prints from the code above to print to console.

I have another object that has RigidBody2D and CircleCollider2D as well. It also has the tag "Projectile". When the collider areas overlap with each other nothing happens.

Enemy: RigidBody2D, CircleCollider2D, C# code above.

Enemy:Tag: "Projectile", RigidBody2D, CircleCollider2D: IsTrigger - true.

CodePudding user response:

You have isTrigger checked in collider component. OnCollisionEnter doesn't work when trigger is enabled. There's another function OnTriggerEnter if you want to use trigger. OnTriggerEnter

OnCollisionEnter

Don't confuse these two.

CodePudding user response:

Have you checked your rigidbody properties?

I know some of the properties that you can change on that effect the way collisions work, try turning off simulated if that one is on for a start.

  • Related