#Optimising checking for a trigger collision

1 messages · Page 1 of 1 (latest)

fallow crest
#

I want to add safe areas into my game, I am using this code on my player to check if he`s safe right now, however I dont think this is optimised and I want to know if theres a better solution to this.

private void OnTriggerStay(Collider other)
    {
        if(other.tag == "SafeArea")
        {
            playerInSafeArea = true;
        }
        if(other.tag == "DangerousArea")
        {
            playerInSafeArea = false;
        }
    }

my scene is set up like its shown in the picture (its 3d)

vale robin
#

you should use comparetag to compare tags

#

and what happens when the player is in both the safe and danger zone

fallow crest
#

Mmh

#

Havent tought about that

#

Should I add an extra case for that

#

Or should I leave a gap between the colliders?

#

Because when Ieave the gap, i can do it with on trigger enter, which would be better for performance right?

zinc sundial
#

Maybe just use one trigger & tag - if the player enters a safe zone, they are safe. When they exit a safe zone, they are not safe.
Why introduce a "state in-between" by looking for both things separately when you have a binary state anyways?

fallow crest
#

great Idea!

#

Thank you!

zinc sundial
#

np

dreamy willow
#

Actually if the player is in both zones, it would be arbitrary, since this code appears to be on the player