Schiytu37
Schiytu37

Reputation: 23

unity c# OnCollisionEnter2D() Not working

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.

Upvotes: 1

Views: 1496

Answers (2)

Isaac Lyne
Isaac Lyne

Reputation: 192

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.

Upvotes: 1

YGreater
YGreater

Reputation: 82

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.

Upvotes: 0

Related Questions