[SOLVED] OnTriggerEnter2D: I got a bug and I don't see a solution.

Hi,

I’ve been making my own little platformer to get to know Unity and now I got stuck with a bug I can’t seem to solve.

What I have so for (quick overview)
So what I have now is 2 players with a rigidbody 2D and 2D Colliders:


The difference between them is my input (from controller), the layer name, tag name and playerID.

Both these players have a child named Headband:

Now this child has a script to fire a ball into the last aimed to location with my joystick. This instantiates a BulletTrail_P1 or P2 prefab.


Again all these objects are the same for both players except for the player id or num.

Now to show you the actual problem:
(video) https://i.gyazo.com/77ad2e4a130dac9309fc1d2fae1f066e.mp4

As you can see, the OnTriggerEnter2D is working for player 1 shooting player 2, but when player 2 fires, he does trigger something on himself but a trigger isn’t made on player 1.

Here’s the code that makes checks for triggers:

using UnityEngine;
using System.Collections;

public class BulletTriggerDetection : MonoBehaviour {
    public int playerNum = 1;

    void OnTriggerEnter2D(Collider2D  col)
    {
      //  Debug.Log(gameObject.name + " has collided with " + col.gameObject.name);
      //  Debug.Log("My layer: " + gameObject.layer);

        if (playerNum == 1)
        {
            if (col.gameObject.tag == ("Player2") || col.gameObject.tag == ("Player3") || col.gameObject.tag == ("Player4"))
            {
               col.gameObject.GetComponent<ShotPlayer>().Hurt(playerNum);
                Debug.Log("Player" + playerNum + " just shot " + col.gameObject.tag);
            }
            if (col.gameObject.tag  != ("Player1"))
            {
                Destroy(gameObject);
            }
        }
        else if (playerNum == 2)
        {
            if (col.gameObject.tag == ("Player1") || col.gameObject.tag == ("Player3") || col.gameObject.tag == ("Player4"))
            {
                col.gameObject.GetComponent<ShotPlayer>().Hurt(playerNum);
                Debug.Log("Player" + playerNum + " just shot " + col.gameObject.tag);
            }
            if (col.gameObject.tag != ("Player2"))
            {
                Destroy(gameObject);
            }
        }
    }
}

The trigger event should fire anytime the bullet hits anything right?
But it seems it doesn’t trigger on player 1 whatever I do. What am I missing?

Thanks allot!

(Please let me know if I left out anything important)

1 Like

have you checked “bullet” layer is set to collide with layer “Player1” in the collision matrix?

1 Like

Wow, this shows how new I am to this, I didn’t even know this existed.

You are e genius! Thanks allot, I’ve been looking at this for 4 hours xD

:slight_smile: