- Home /
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?
Answer by highpockets · Feb 11, 2014 at 07:29 PM
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
I don't understand how that would be done. I have a prefab Named "Hoop". It has a collider for the edges of the ring so you don't go through the edges. It already has a tag "Hoop". Then I add a new collider in the middle that registers that the player has gone through it. I mark the inner collider as an IS Trigger. But how do I then tag that collider?
This should not be written as an answer, if you have more questions, please ask them via comments.. Anyways, the hoop is a game object and I assume that the collider attached to the hoop is a mesh collider. So, you have 2 ways of doing this:
First option.... This is what I would do. Add an empty game object and place it in the middle of the hoop. Add a box collider ( or other collider, such as a sphere to better represent the shape of the hoop, but keep in $$anonymous$$d that box colliders are the least expensive ) component to the new game object and scale it to size inside the hoop. Add the tag to this new game object... You can use the OnTriggerEnter function on a script attached either to the player or the new game object. Finally, ensure that this game object is a child of the hoop if the hoop moves around at all and it will match the transform of the hoop.
Second option..... You can also add the collider to the existing hoop Game Object. You will have to have something like this added to your player script ins$$anonymous$$d of tagging it:
private BoxCollider hoopTrigger;
void Start()
{
hoopTrigger = GameObject.Find("The name of you hoop object").GetComponent<BoxCollider>();
}
void OnTriggerEnter( Collider other )
{
if( other.collider == hoopTrigger )
{
//Do something...
}
}
So... If I want to tag any components, they must be part of a game object? I can't tag components within a game object can I? Technically I only tag the game object and every child of that game object will get the same tag?
Answer by rcfearn · Oct 11, 2017 at 11:08 AM
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'.
Your answer
Follow this Question
Related Questions
Often when loading level the Layers i have on gameobjects reset to default. Please help? 1 Answer
Head shoot and body shoot not working with realistic fps characterDamage 0 Answers
Simple code is not recognizing tag anymore. 0 Answers
Player attack doesn't recgonize different colliders (unity 2D) 1 Answer
Make player face different enemies with the same tag 0 Answers