Home > database >  Why is my void CollisioEnter/TriggerEnter isnt detecting
Why is my void CollisioEnter/TriggerEnter isnt detecting

Time:11-06

So i recently completed a tutorial on how to make a third person camera view, i want to use that project to detect any collision happening to the player by writing this line of code below:

using UnityEngine;

public class CheckCollide : MonoBehaviour
{
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Gem") //Im making sure if the player hit the object with the tag"Gem"
        {
            Destroy(other.gameObject);//I want to destroy the gem after collide with the player
        }
    }
    
    void Update()
    {

    }

}

but sadly it didnt seem to work, so how can I detect the collision between them? thankyou so much for reading this far! have a nice day kind random guy! ^^

FYI:

I made the player inside of a parent gameobject, and so do the Gem, image below: enter image description here

CodePudding user response:

Here is how you can make sure that you are getting the right collision set up.

First off, your Player or your Gem objects need the component Rigidbody. If you don't want the rigidbody to affect the object, set kinematic to true and/or freeze constraints.

Nextly, both of your objects (Player and Gem) need a collider of some sort. I recommend Box Collider. On one of the Box Colliders, check mark IsTrigger. This way one of your objects can be used as a trigger. I'd recommend making your Gem object a trigger.

If none of this is working, double check that you have the tags in your script correctly, and that your Gem object has the right tag set for itself.

  • Related