Tagging a collider?

I am making a game where the player has to jump through a series of hoops that are spawned at random heights. Each time they jump through the hoop they get a point. I already have a collider on the outer side of the hoop but I have no idea how to register if the player has gone through the hoop. I wanted to add a collider in the middle with a tag but it seems that you cannot do that. How do I register if the player has gone through the hoop?

Use a collider and mark it as a trigger. In the inspector, you will see the collider component with an isTrigger check box, make sure that is checked.

Then just below the game object name, in the inspector, there is a drop down menu named “Tag”, click that and choose add new tag.

To the right of “element 0” in the tag manager, click the empty slot and write your tag as something like: “Hoop Trigger”

Then, on the script attached to your player add this:

C#

void OnTriggerEnter( Collider other )
{

if( other.gameObject.tag == "Hoop Trigger" )
{

//Do Something....
}

}

JS

function OnTriggerEnter (other : Collider) 
{
	
if( other.gameObject.tag == "Hoop Trigger")
{

//Do Something...
}
}

That should do the trick

You can easily do this by adding each specific collider to an empty object (probably children of a main empty object) because then you can add a tag to each empty object. Your actual visible model may not even need a collider or may just have a general catch-all collider.

If you wanted a Cube to have 6 specific colliders (one for each side) you could do something like this

MyBox (Empty Object tagged with ‘MyBox’)

  • Cube (Visible Object/Model - No colliders)
  • TopColliderObject (Empty Object w/ box collider positioned at top of cube and tag e.g. ‘Top’)
  • LeftColliderObject (Empty Object w/ box collider and tag ‘Left’)
  • and so on…

Then in your code you can do something like:

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.CompareTag("Left"))
        {
            rigidBody.velocity = new Vector3(1, 2, 3);
        }
    }

and if you want to test for the parent tag you can easily get that as well:

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.transform.parent.gameObject.CompareTag("MyBox"))
        {
            if (collision.gameObject.CompareTag("Left"))
            {
                rigidBody.velocity = new Vector3(1, 2, 3);
            }    

            if (collision.gameObject.CompareTag("Top"))
            {
                rigidBody.velocity = new Vector3(2, 1, 3);
            }    
        }
    }

It would probably be neater to use a switch statement if you’ve got lots of colliders/tags and you may also need to do some null reference checks on ‘parent’.