- Home /
Is There A Way To Disable a Trigger But Still Use OnTriggerExit2D? (For COOP switch management)
Hi everyone!
I'm making a 4 player coop game and I'm running into problems with trigger based switches, where more than one player can activate the switch at a time. I've tried lots of different workarounds include using bools that coincide with a player's ID string but I just can't seem to get a grasp on the logic. I need a way to disable a trigger (or keep other players from using it) while one player is inside the trigger but I'm having a hard time. I've tried a triggerEnabled bool which is made true when the player presses a button while in the trigger and then used in an if statement that is supposed to return if this bool is true. In my mind that should effectively render the trigger useless to other players while someone is already inside it but its not working. Example code:
private bool triggerEnabled;
void OnTriggerEnter2D (Collider2D col)
{
if(col.tag == "Player" && !triggerEnabled)
{
triggerEnabled = true;
//Do Some Other Stuff
}
if(col.tag == "Player" && triggerEnabled)
{
return;
}
}
I'd be very grateful if anyone could tell me why this logic is flawed and/or offer a solution! I appreciate it!
Answer by NAiLz · Jul 03, 2017 at 01:50 PM
@PigletSoft
Ok, I think I found a solution. It's working so far with no bugs. Basically I created a new GameObject called playerInTrigger then checked to see if that gameobject is null or not in OnTriggerEnter2D and in OnTriggerExit2D I compared any new Colliders to the one already inside the trigger. Code:
private GameObject playerInTrigger;
void OnTriggerEnter2D (Collider2D col)
{
if(col.tag == "Player" && !triggerEnabled && playerInTrigger == null)
{
playerInTrigger = col.gameObject;
//Do Other Stuff
}
else
{
return;
}
}
void OnTriggerExit2D (Collider2D col)
{
if(col.tag == "Player" && triggerEnabled && col.gameObject == playerInTrigger)
{
playerInTrigger = null;
//Do The Other Things
}
}
(I left the triggerEnabled bool in there because I still use in it Update to check for player input. ;)
Anyway, hope this helps someone out in the future.
Answer by IronPig_ · Jul 03, 2017 at 01:13 PM
I think you should create two layers for the players like CanUseTrig and CantUseTrig. The CantUseTrig layer should not collide with the layer of the trigger but the other should.
By default every player should be on the CanUseTrig layer, but if one of them enter the trigger all other should go to the CantUseTrig and when the player exit the trigger everything would go back to normal.
This would be a great solution if there wasn't a need to have multiple switches at once. An example would be my character selection scene. There are 4 cryogenic suspension chambers, each with a character inside and the player(s) use a proxy character to walk around the scene and select a cryo chamber as a way of selecting which character they want to play. If I went with the layers method, the other players wouldn't be able to select characters. But I like this idea alot!
I think I found a simple solution. Posting below.