- Home /
Layermask for OnTriggerStay
How do I check if the colliding object on OnTriggerStay is of a certain layer? I used this and it didn't quite work:
void OnTriggerStay(Collider Player) {
// If the player is the colliding object.
if(Player.gameObject.layer == 9) {
}
}
Any help is appreciated.
The all of it part... I tried to use != "Default" but it didn't work and still registered the floor.
Are you sure the object is on layer 9? In any case, I'd recommend using Layer$$anonymous$$ask.NameToLayer to avoid confusion.
How do you know it isn't working? There is no code being ran after the if statement, try adding a log output or something.
If I delete the filter the code works but it registers everything as the player. When I added the filter it stopped working.
Answer by Piyush_Pandey · May 11, 2017 at 06:38 AM
There might be a chance that it is not getting inside the OnTriggerStay method. To make sure it works well, please don't forget to add a collider with isTrigger enabled. Also dont forget to that rigidbody has to be attached to one of the two colliding objects. If all of this is already there, try using just like Arkaid in the comments said.
int layer_index;
void Start()
{
layer_index = LayerMask.GetMask("Layer_name_you_want");
}
void OnTriggerStay(Collider Player)
{
// If the player is the colliding object.
if(Player.gameObject.layer == layer_index)
{
}
}
Answer by DrBraulio · May 11, 2017 at 02:05 PM
I recomend you that take a look about Overlap Box whit this you ll have more control in your layersMask target remember to use too DrawGizmos (see your collider limits in the scene) take care about parameters because are little bit differents.