Engineer627
Engineer627

Reputation: 5

OnTriggerEnter Colliders In Unity3D C#

I am very confused on how OnTriggerEnter works, or Colliders in Unity in general. I am trying to add a collision test that, if the bullet hits an object, it plays a particle system and debugs something, but it's not working and I am very confused on why. Help please.

Here is my code:

public class bulletScript : MonoBehaviour {

public ParticleSystem explosion;

public float speed = 20f;
public Rigidbody rb;

bool canDieSoon;

public float timeToDie;

void Start()
{
    canDieSoon = true;
}
// Start is called before the first frame update
void Update()
{

    rb.velocity = transform.forward * speed;

    if(canDieSoon == true)
    {
        Invoke(nameof(killBullet), timeToDie);
        canDieSoon = false;
    }
    


}



private void OnTriggerEnter(Collider collision)
{

    if (collision.gameObject.tag == "ground")
    {
        Debug.Log("hit ground");
        explosion.Play();
    }
    if (collision.gameObject.tag == "robot")
    {
        Debug.Log("hit ground");
        explosion.Play();
    }
    if (collision.gameObject.tag == "gameObjects")
    {
        Debug.Log("hit ground");
        explosion.Play();
    }
    if (collision.gameObject.tag == "walls")
    {
        Debug.Log("hit ground");
        explosion.Play();
    }
    if (collision.gameObject.tag == "doors")
    {
        Debug.Log("hit ground");
        explosion.Play();
    }
}




void killBullet()
{
    Destroy(gameObject);
}

}

Upvotes: 0

Views: 15899

Answers (1)

Thomas Finet
Thomas Finet

Reputation: 272

Colliders may seams a bit tricky at the beginning but let me explain the basics to you.

First, to receive any function like OnCollisionEnter or OnTriggerExit the gameObject where those function would be called has to get a Collider component (2D or 3D in any shape you want depending of your project) attached to it.

Then, if you wanna work with triggers, you have to check the "is trigger" checkbox on the collider component. In addition of that, one or both of the colliding object must have a rigidbody

Finally you ccertainly want to check the bottom of Unity Collider manual which contains the collision matrix of Unity, describing what can or cannot collide with what.

In addition, to help you develop your coding skills, I would recommend you to use some loops in your OnTriggerEnter function to stay DRY

private void OnTriggerEnter(Collider collision)
{
    string[] tagToFind = new string[]{"ground", "robot", "gameObjects", "walls", "doors"};

    for(int i = 0; i < tagToFind.Length; i++)
    {
        var testedTag = tagToFind[i];
        //compareTag() is more efficient than comparing a string
        if (!collision.CompareTag(testedTag)) continue; //return early pattern, if not the good tag, stop there and check the others
        
        Debug.Log($"hit {testedTag}"); //improve your debug informations
        explosion.Play();
        return; //if the tag is found, no need to continue looping
    }
}

Hope that helped ;)

Upvotes: 5

Related Questions