- Home /
General beginner question about collisions/triggers
I've just set up an invisible trigger in a doorway that checks for a collision with the player (if col.gameObject == dude...) and executes a music crossfade coroutine. No problems here, but I learned along the way that Unity's OnTriggerEnter checks for a collision with ANYTHING, and then you have to use ifs to narrow it down. My first instinct was to define a specific collider and use it as a parameter with OnTriggerEnter, which didn't work of course. This approach seems counterintuitive. For instance, if my invisible doorway collider is touching the walls and floor, isn't that going to waste CPU cycles? Or is it just as spendy as having a trigger hovering in midair? Do I need to care about that stuff?
Answer by YoungDeveloper · Jun 02, 2015 at 07:34 AM
Collision and Trigger check should in most cases be placed once - on player itself. So you check them once, and not for each door separately. You can use tags, layer masks or script with data attached to it to filter what you need.
Answer by KoJix · Jun 02, 2015 at 07:38 AM
You can give your player a tag like Player and say in code:
void OnTriggerEnter (Collider other){
if (other.tag.Equals("Player")){
Dosomething();
}
}
About a collider intersecting with multiple object. So far as I know, It wont be effecting anything this much you've to worry about it.
Is it cheaper to check for tag than gameObject? I only want it to react to one object anyway.
I don't know how expensive various operations are on the CPU, so I get paranoid about efficiency. It seems like if you had a lot of colliders in a level, there'd be a lot of wasteful checking for certain tags etc as they touch unrelated things.
Tags are a easy and fast way to detect if your triggers are colliding with the right gameobject. Colliders will be seen as physics, physics rendered by the CPU will not have a lot of cost.
Your answer