- Home /
Checking if colliding with anything in a layer
Hello to the people of unity answers!
I am trying to figure out how to make my player character in a 2D game die whenever they aren't colliding with any colliders in a certain layer, effectively creating a boundary they must stay inside. The problem is there are multiple overlapping colliders in the layer and simply killing the player upon exiting one won't work. Here is a small bit of code:
void Update ()
{
if (boundariesTouching < 1 & respawning == false) //If the player is not in a boundary and not respawning then kill them.
StartCoroutine (Respawn ()); //Teleports the player back to origional start point after short delay and animation.
}
void OnTriggerEnter2D (Collider2D collider)
{
if (collider.gameObject.layer == safeLayer) //If the player enters a boundary add one to the boundariesTouching variable.
{
boundariesTouching++;
print ("Player is in " + boundariesTouching + " boundaries");
}
}
void OnTriggerExit2D (Collider2D collider)
{
if (collider.gameObject.layer == safeLayer) //If the player leves a boundary subtract one from the boundariesTouching variable.
{
boundariesTouching--;
print ("Player is in " + boundariesTouching + " boundaries");
}
}
The problem with this system is that when the player falls out of the boundaries, they are teleported back to the original starting point, which for some reason only sometimes doesn't count as colliding with the boundaries, and kills the player again. The only reason I can think it does this is when the player dies and is teleported back to start, the player's rigidbody simulation is turned off for a brief time. I don't know how to stop the rigidbody from being simulated while still letting colliders function.
Other than raycasting into the layer every frame, I'm clueless here.
Is it really necessary to have the player inside the trigger all the time and kill it when going out? Couldn't you just place four triggers to create a square and if the player touches any of those edges then he dies?
It seems it would be easier to create levels by placing large boxes where they can go... but if there is no other way then I guess I'll do that.
It does seems it would be easier at first glance I mean using only 1 trigger sounds better than using 4 but code wise using only 1 trigger has more edge cases to handle, if you have 4 triggers you only have to worry about OnTriggerEnter2D and that's it, no Update check and no Exit check.
Answer by TheShrunk · Sep 09, 2017 at 12:32 AM
After modifying how the player respawns I solved it. It turns out that turning off the rigidbody's simulation did indeed cause the collision to not register.
Your answer
Follow this Question
Related Questions
get only one colliding body 3 Answers
OnTriggerEnter being called multiple times 0 Answers
OnTriggerEnter called much less often after a player respawn? 1 Answer
How do I use colliders and/or triggers to end the game? 1 Answer
How do you avoid collisions when teleporting CharacterControllers? 0 Answers