Loading level when two characters are in respective trigger areas.

I’m working on a local co-op game in which both characters have to enter a square – each one on their respective screen – in order to progress to the next level (similar to Thomas Was Alone, in that regard, only with split screen). However, what currently happens is once either player enters the trigger area, the loading sequence to the next level has been initiated.

 public GameObject playerOne, playerTwo; // Declares p1 & p2 for ease of prototype

 private bool isTrigger = false; // Checks if player is in trigger zones

 void Update () {
 	 if (playerOne && playerTwo && isTrigger == true) { // Is p1 & p2 in the trigger area?
		 Debug.Log ("loading");
	 }
 }

 void OnTriggerEnter () {
	 isTrigger = true;
 }

 void OnTriggerExit () {
	 isTrigger = false;
 }

I think I know what’s going wrong here. If I’m right, it’s because the if/and in Update is… well, to be polite, it’s wrong and not what I want it to do.

One way I thought about overcoming this was by declaring an int (exitKey) and running a for loop to increment 1 when p1 enters the area, and again when p2 enters the area, but I think that will increment 1 each time either player enters the area (so p2 could enter the area, exit it, and then reenter it in order to load the next level).

If somebody could point out where I’m going wrong and what I can do to get it working as intended, it would be very much appreciated!


SOLUTION

Thanks to BoredMormon I’ve managed to solve this and thought I’d put my solution here for those who many have this issue in the future.

I have three scripts - PlayerOneMovement, PlayerTwoMovement, and ExitLvl. On both the player scripts, there is a bool (ExitTrig) which is false until the players enter the trigger area.

 void OnTriggerEnter (Collider other) {
	if (other.tag == "exitOne") {
		exitTrig = true;
	}
 }

void OnTriggerExit (Collider other) {
	if (other.tag == "exitOne") {
		exitTrig = false;
	}
 }

As both players have an exitTrig bool on each of their respective scripts, the ExitLvl script checks for this and, when they both return true, triggers the level load:

public PlayerOneMovement p1Script; // allows me to access
public PlayerTwoMovement p2Script; // bools on player scripts

private bool nxtLvl = false; // checks whether we can load next level

void Start () {
	p1Script = GameObject.Find("playerOne").GetComponent<PlayerOneMovement>(); // accessing playerone's script
	p2Script = GameObject.Find("playerTwo").GetComponent<PlayerTwoMovement>(); // accessing playertwo's script
}

void Update () {
    // checks if we can load the next level
	if (nxtLvl == true) {
		Debug.Log ("loading");
	}
}

void OnTriggerStay () {
    // checks if both players are in trigger zone and if so, sets true, else false.
	if (p1Script.exitTrig == true && p2Script.exitTrig == true) {
		nxtLvl = true;
	} else {
		nxtLvl = false;
	}
}

void OnTriggerExit () {
    // if neither are on triggers, sets false
	if (p1Script.exitTrig == false && p2Script.exitTrig == false) {
		nxtLvl = false;
	}
}

It’s far from perfect at the moment, but as this is largely a prototype, it’s workable for the moment. Thanks for all the help, people!

Quick solution:

public bool isPlayerInTrigger = false;
public PlayerComponent playerOne, playerTwo;

if (playerOne.isPlayerInTrigger && player2.isPlayerInTrigger ){
    Application.LoadLevel(1);
}

Your current code logic is messed up. Line 6 checks if both players exist, and the current player is in the trigger. You should also rename isTrigger as it can easily be confused with the unity variable of the same name.

You can check if there is a player in a certain area in the OnTriggerEnter Method. Also check the Scripting Reference Unity - Scripting API: MonoBehaviour.OnTriggerEnter(Collider) how it actually works.