- Home /
Rigidbody + Multiple Trigger Colliders - OnTriggerEnter - Get which Collider triggered the event issue
Good morning, unityAnswers. First time poster - I'll try to make myself clear.
I want a character in my game to have a cylindrical collision box with a rigidbody. With the only options similar to what I want being a BoxCollider or a CapsuleCollider, I'm trying to combine the two and using them as triggers - making the CapsuleCollider higher and lower than the height of the BoxCollider, thus the areas that collide with BOTH make out a cylinder.
OnTriggerEnter / Leave get called when either one or the other enters or leaves another CollisionBox, but the Collider object sent to these methods only holds information for the object hit by my character.
In no way have I been able to pull which of my two colliders triggered the event, which is necessary for me to track whether both are colliding at the same time.
I turn to you, Unity Community - as I have been using all your questions and answers frequently - to much aid.
Thank you all in advance, if further clarification is necessary, please ask and I'll do my best.
Cheers.
Answer by Owen-Reynolds · Aug 02, 2012 at 04:00 PM
Anytime you need to check for collider contact, but can't do it in the enter function, you can have them set flags, so the triggers are recording your in/out status. Ex of cubeTriggerScript:
bool inCube=false;
OnTriggerEnter(..) { if(player) inCube=true; }
OnTriggerExit(..) { if(player) inCube=false; }
Then do the same with, say, inCapsule
and the capsule trigger.
But this gives up the advantage of Enter/Exit knowing we just now entered. To get that back, you need one more flag to remember if you were in or out last frame. You could then have Update check something like:
bool inCyArea=false; // make sure I do stuff only the 1 frame I enter/exit
if(!inCyArea) { // if I'm currently not in, did I just enter:
if(inCube && inCapsule) { inCyArea=true; do enter area stuff }
}
else // I'm currently in, looking to get out:
if(!inCube || !inCapsule) { inCyArea=false; do leave stuff }
More efficient is to move that from every-frame update to each enter/exit (which is the only time you need to check.) Ex:
OnTriggerEnter(..) { if(player) { inCube=true; checkForNewEntExStatus(); } }
Of course, the two triggerAreas will have separate scripts for their Enter/Exits. That means you'll really be reaching all over the place for the variables, with things like boxScript.inCube
instead of a simple inCube
.
Hi Owen, thanks for your quick reply!
I understand what you mean, I was simply hoping there was a workaround that would allow me to monitor both of the collision boxes in the same script. It's a shame I can't.
However, since I've posted this question, I've created a cylinder mesh in Blender, and added it as a $$anonymous$$eshCollider - having some issues with it colliding with other object's colliders such as BoxColliders, but I'm working that out.
Thanks again for your input, if you have any other tips for a man in this situation, feel free to suggest!
Cheers.
The meshColliders now work surprisingly well. $$anonymous$$ake sure you check Convex (and don't go nuts on the poly count.)
I used the trick with bools to make a "shell" around something, which can't be done with a convex mesh.