- Home /
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!
You also might want to keep in $$anonymous$$d to check what entered the trigger with something like this. For example I oftin check the tag and see if it is the player. If it is, then make some variable change.
Answer by Kiwasi · Sep 08, 2014 at 11:36 PM
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.
I figured there was something wrong with the logic, and thanks for the heads up regarding 'isTrigger'. I was just quickly prototyping and it didn't even occur to me.
Just had a check of your example and it seems that when playerOne and Two are used in tandem with dot syntax, it doesn't recognise the isPlayerInTrigger variable. Likewise with "PlayerComponent". Any ideas?
This was pseudo code meant to get you on the right track. Won't copy over literally into your project.
In essence you must use two bools, each bool tracks if one player is in one trigger. Then your if check must check if both bools are true.
I'd assumed the bool variables were located on your player. Hence the generic PlayerComponent. Replace that with the name of whatever class has the bool. isPlayerInTrigger needs to be replaced with the actual name of the bool variable.
Ah right! Sorry, I'm still reasonably new to C#, so I wasn't sure if it was something I just haven't came across yet. Thank you very much!
Answer by Scario · Sep 09, 2014 at 02:51 AM
You can check if there is a player in a certain area in the OnTriggerEnter Method. Also check the Scripting Reference http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html how it actually works.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Activate/Deactivate UI Image using C# 0 Answers
Trigger not getting GameObject 2 Answers
X number of collisions for destroy 3 Answers