- Home /
Trigger reacts to multiple colliders not the object
void OnTriggerEnter2D(Collider2D collider){
if(collider.gameObject.CompareTag("Car")){
LapManager.instance.Check(transform);
}
}
My car have one block collider and two circle colliders. The problem is that every of them triggers the script and what I want is to make trigger react to GameObject one time per touch. Is there a good way to make this? I was thinking about list of gameObjects that are inside trigger at the moment, so I'll check if gameObject is in the list already before actions. I hope there is a better way.
If I understand correctly, you want the trigger to fire when the first one collides, but ignore the others? Will the objects collide with this trigger again later? (like for counting laps)
Exactly. Yeah it will collide that checkpoint on the next lap
Could you modify your car object so that the block collider has a different tag than the two circle colliders? This way you could check for just one collider on collision (probably the block collider) by its tag and not worry about the two circle colliders.
[Edit] Alternatively, if you don't want to change the tags on your objects, you can check the name of the object you hit. For example, let's say your block colliders will always be attached to objects named "Car#" (where # is the number of the car), and your circle colliders will always be attached to objects named "wheel". You can do the following check in your code:
if(collider.gameObject.name.Contains("Car"))
to ensure that only the car object will trigger the event.
so basically, I added a child to my car and put circle colliders on it. As this child does not have tag "Car" it will not trigger my script. Thank you! It is very simple, but is better than modifying my script with list etc.
Answer by BBoldenow · Jun 19, 2014 at 06:32 PM
Another way to do this is with layers. You can create a new layer for the trigger and collider. Then in your physics manager don't let this layer interact with any other physics layer. This will be more efficient as well since the trigger and collider aren't going to have to check against every other object in the scene.
Your answer
Follow this Question
Related Questions
Collider IsTrigger won't work correctly with close second Collider 0 Answers
Can't click gameobject when over another trigger? 1 Answer
Collider Activate Trigger Animation 0 Answers
OnTriggerEnter not being called with multiple colliders 1 Answer
Triggering platform animation on colliding with Button 0 Answers