x


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?

more ▼

asked Sep 30 '11 at 04:18 PM

Matt Woods gravatar image

Matt Woods
66 9 9 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 

This is the TriggerScript.js:

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;
    }
}
more ▼

answered Oct 01 '11 at 03:06 AM

aldonaletto gravatar image

aldonaletto
41.3k 16 42 195

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.

Oct 03 '11 at 02:44 PM Matt Woods
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1683
x980
x25

asked: Sep 30 '11 at 04:18 PM

Seen: 814 times

Last Updated: Oct 03 '11 at 02:44 PM