- Home /
When two triggers intersect, when are enter and exit events called?
So, in my game I have a number of objects that send messages to the player prompting them to take a closer look. I originally did this by having a large spherical trigger collider around the object that activated when they came close. In playtesting, people wanted that message to appear only when the player is looking at the triggering object.
At first I thought I had to do some complicated 3d math to determine the direction that the character is facing in relation to the object, then I thought of another way to do it. Instead of a big spherical trigger around the object, I instead made a small one, and attached an invisible cone shaped trigger (based on line of sight) to the player. My objects trigger checks OnTriggerEnter, and OnTriggerExit to see if the activating trigger is the line of sight cone, and if so calls the message function.
Unfortunately, this doesn't seem to be working. When my sight cone intersects the objects cone, the object receives an OnTriggerEnter, but then immediately receives an OnTriggerExit. I'm not sure why I am getting the OnTriggerExit call, as the two triggers are still intersecting. I need it to not send the exit until the player looks away, and the triggers are no longer intersecting. Any ideas what is going on, and how to work around?
Answer by aldonaletto · Oct 01, 2011 at 03:06 AM
Maybe the problem is caused by some bug in the object you're using as the conic trigger. I don't know how you can solve this, but if you want to switch back to the spherical triggers, the 3D math is very simple - attach this script to the player:
var viewAngle: float = 20; // view angle in degrees
function OnTriggerStay(col: Collider){ var tScript: TriggerScript = col.GetComponent(TriggerScript); if (tScript){ // if the trigger has a TriggerScript.js attached: var objDir = col.transform.position - transform.position; var angle = Vector3.Angle(transform.forward, objDir); tScript.inViewAngle = angle
var inViewAngle: boolean = false;
function Update(){ if (inViewAngle){ // show the message } else { // hide the message } }
// ensure message off when the player exits the trigger function OnTriggerExit(col: Collider){ if (col.tag == "Player"){ inViewAngle = false; } }
Hi Aldonaletto,
Thanks for your answer and script. Good to know the math to do that. I ended up solving my problem by discovering the renderer.isVisible command.
I still don't know why my previous trigger code wasn't working. I think its because I had the trigger on the smaller collider, and that there is a bug/feature where "onTriggerExit" gets called on a small trigger when that small trigger completely enters a larger trigger. I'll have do more to test that theory.
Your answer
![](https://koobas.hobune.stream/wayback/20220612232052im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
OnTriggerEnter mesh collider problem 1 Answer
Enemy line of sight using linecast and colliders 1 Answer
Rigidbody Trigger 1 Answer
GameObject Parent & Child Visibility 0 Answers