- Home /
Detect 2 colliders by trigger
I am trying a system of covering and I have walls that have 4 colliders and 2 walls that 2 colliders get together and I want that if you get inside the 2 colliders the TriggerStay can identify that there are 2 colliders and to choose the new collider he just is trigger. I haven't see better the ContactPoint.otherCollider but it seems that's what I am looking for but it seems it only works for OnCollision not with OnTrigger so please someone help me to know a way to know if the player is in 2 colliders and if it is to choose the new collider he enter. I want something like this:
function OnTriggerStay(other[] : Collider){//obviously all the code is wrong
if(other[1]/*exists trigger with player*/){
//player.direction = newcollider;
}
}
Answer by aldonaletto · Aug 25, 2011 at 01:05 AM
You can put the OnTrigger event in the Player script. If you enter a region where two triggers intersect, both triggers will call OnTriggerStay, so you will have two events at each cycle.
But I think it would be better to use OnTriggerEnter and OnTriggerExit:
var trig1: Collider;
function OnTriggerEnter(trig: Collider){ if (trig1){ // if player already in one trigger... // player goes to the new trigger trig.transform.position } else { // it's the first trigger trig1 = trig; // save its reference in trig1 } }
function OnTriggerExit(trig: Collider){ if (trig1==trig){ // if player left trigger 1 trig1 = null; // clear trig1 } }
ouhh, I get it, I haven't tested but seems good in logic. Thanks.
trig1 "borns" with null value (but you can initialize it to null, if you want to be sure). When the player enters the first trigger (this script is assigned to the player) the script stores the trigger reference in trig1, thus when entering the second trigger the player will know it's already inside trigger 1 (trig1 is zeroed when the player leaves the first trigger). This works in any direction, since the first trigger entered sets trig1, no matter which one it is.