Question on the use of Bullet + OnTriggerEnter

By using OnTriggerEnter2D and not setting the Bullets as Trigger, we’re detecting hit using the BoxCollider2D part of the periscope, right?
Shouldn’t this way prevent us to shoot the enemies from their back?

Hi,

Collision always happen between colliders. Unlike the EdgeCollider2D, the other colliders have an inner part. For this reason, we always use the entire collider shape if we use a BoxCollider2D.

Trigger colliders do not have a “solid” body, so collisions with trigger colliders do not cause any “physical” behaviour. It fires an event, though: the trigger event which calls the OnTrigger*2D methods of the game objects involved in the current collision.

The bullet is able to move through the BoxCollider2D (trigger), and it won’t get affected by anything unless a trigger event gets fired which invokes a method that destroys the bullet. If there is no such an event/method, the bullet will move through the trigger collider and hit the capsule collider.

Is this what you wanted to know?


See also:

1 Like

Kinda, I understood that, in absence of a script that destroys it OnTriggerEvent, the bullet:
-moves trough colliders set as trigger
-doesn’t move trough collider not set as trigger.

I tried making the image a bit clearer: so, is this the case for tilevania enemies?


If I shoot them from the capsule collider’s side I won’t be able to kill them, because the bullet will be stopped by the capsule collider (since it’s not a trigger will have a solid body).

(A possible workaround could be setting bullets as trigger)

PS:Thanks for taking the time to answer!

Is the bullet a non-trigger? If so, the bullet will bounce off the non-trigger collider (capsule) if there is no collision event method that destroys it.

In the end, you have to think about the desired outcome:

  • Which event method do you want to use in your game logic?
  • Which collider types do you need to make that event method get called? (See the table at the bottom of this webpage.

There are multiple ways to achieve the desired result. Making the bullet a trigger, could solve the problem (= destroy enemy with bullet), too.

1 Like

I went back to check and it seems that in Rick’s case the bullet is not a trigger, and by following his method I can’t seem to kill enemies when shooting from their capsule collider’s side.
This is Rick’s:

By setting bullet as trigger I solved this problem, but I made this post to check wether this was a known issue/feature or wether I did something wrong, because, back on Rick’s video, it seems that he can kill enemies from behind. So I guess I’m missing something: :thinking:
-Rick’s: (I drawed the two colliders, since the game view’s doesn’t show them, green is capsule collider and orange is box collider (isTrigger))


-Me:

Relevant code:

 void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.tag == "Enemy")
        {
            Destroy(other.gameObject);
            Debug.Log("Destroying enemy");
            
        }
        Destroy (gameObject);
        Debug.Log("destroying bullet");
    }

According to your video, the capsule collider is a non-trigger. If the bullet is a non-trigger, too, the OnTriggerEnter2D method won’t get called when the two colliders collide with one another.

You set up the collision matrix and the layers correctly, didn’t you? See video “Instantiate Bullet From Gun” and the following.

By the way, you could download Rick’s project and open it in Unity if you want to analyse it further.

1 Like

Thanks!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms