Hector Astrom
Hector Astrom

Reputation: 33

Why is my bullet moving through my enemy and not detecting a collision?

I have my Unity 2D project set up so that the bullet is a trigger and will detect and collisions but not interact with the environment, and my enemy being a collider so that it will do both. I don't know if the fact that they are two different types affects collisions, but I thought I'd include that detail incase it did. I know this issue is not caused by my bullet moving too quickly since when I turn the speed of the bullet down the issue persists. I will include the code for the enemy collision and the bullet collision detection here:

void OnCollisionEnter2D (Collision2D coll)
{
    if (coll.gameObject.tag == "Proj") {
        Destroy(gameObject);
        Debug.Log("Hit");
    }

Now for the bullet:

void OnTriggerEnter2D(Collider2D hitInfo)
{
    if (hitInfo.gameObject.tag == "Enemy") {
    Destroy(gameObject);
    }

The bullet is detecting a collision with an enemy and deleting itself, but the enemy is not detecting a collision with the bullet and taking damage/deleting itself. Even when I turn off the Destroy(gameObject) part of the bullet code so that it doesn't destroy itself on collision I still have the same problem so I know it isn't matter of it deleting before anything detects. I hope I was thorough enough with my explanation and that someone can help me resolve this issue. Image of the inspectors: https://i.sstatic.net/E4O0l.jpg

Upvotes: 1

Views: 3130

Answers (3)

Guest
Guest

Reputation: 182

I'm not entirely familiar with trigger behaviour, as it's been a while since I've played around with those, but this bit of documentation for MonoBehaviour.OnTriggerEnter2D(Collider2D) is an important starting point for solving your problem:

Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached.

I can't say for sure, but your screenshots suggest that there is no Rigidbody2D involved.


Interestingly,

documentation for Collider2D.OnTriggerEnter2D(Collider2D) neglects to specify that a Rigidbody2D is necessary. I'm not sure what this implies; both MonoBehaviour.OnTriggerEnter(Collider) and Collider.OnTriggerEnter(Collider) (the 3D analogues) are said to require a RigidBody. Both of these pages also claim

If both GameObjects have Collider.isTrigger enabled, no collision happens.

However, the example code from Monobehaviour.OnTriggerEnter2D(Collider2D) claims that setting both 2D colliders as triggers will still allow the function to be called.

Upvotes: 0

vasmos
vasmos

Reputation: 2586

The bullet needs to be a non-trigger collider as you are calling OnCollisionEnter and not OnTriggerEnter from the enemy script.

Upvotes: 1

ChilliPenguin
ChilliPenguin

Reputation: 715

Please show the settings of each collider, also OnCollisionEnter2D and OnTriggerEnter2D are 2 different things, and will occur in different conditions, so make sure that you are using the right one for the player.

Upvotes: 0

Related Questions