- Home /
Do "whatever" when two colliders enter a trigger
I will try to make it brief as possible.
There are two main characters in my game, and I want something to happen when they -both- enter (or stay in) a trigger at the same time.
for example: When both players enter a certain area, a door will open, or an object will be destroyed etc.
Both characters are tagged with "Player".
Answer by robertbu · Aug 22, 2013 at 04:21 AM
Doing it with colliders would be possible but a bit ugly. Here is an alternate solution. The 'WithinRadius()' function will return true if the pivot points of the all game object with a 'Player' tag are within a specified radius of the game object that has the following script. There are possible efficiency improvements that should not be necessary for for just two 'player' objects.
#pragma strict
var radius = 1.5;
private var players : GameObject[];
function Start() {
players = GameObject.FindGameObjectsWithTag("Player");
}
function Update() {
if (WithinRadius()) {
Debug.Log("All player within radius of this game object");
}
}
function WithinRadius() : boolean {
for (var i = 0; i < players.Length; i++) {
if (Vector3.Distance(players[i].transform.position, transform.position) > radius)
return false;
}
return true;
}