- Home /
How can I let a trigger detect if a collider is inside (at start)
Hi everybody
I am currently building a game about being on another planet and such... Whenever the player is in a safezone, the player will be able to recharge the oxygen they are carrying around. I've got the safezone setup, but I only want it to be able to work whenever a oxygengenerator is inside of it.
Since the building in which the oxygen generator is able to stand in, is a prefab (just as the oxygen generator), I want to be able to my every safezone/building to check for themself if an oxygen generator is placed in the safezone/building.
How can I achieve this?
Thanks upfront!
EDIT
I might forgot to mention that the player is a seperate game object. The player is able to walk in and out of the safezone, though the generator cannot move when playing the game. (It can be build however).
I've tried using OnTriggerStay with a tag, but this is not working. By default, some zones already have some generators in them.
Simply use OnTriggerStay if you want to stay with triggers. Ifor not you can use tags and distance. Which in my opinion is less effective
@fluxhackspro I thought of that but doesn't that require one of the objects to move? Because whenever I load up a safezone with a generator already in it, it won't detect it...
Answer by tormentoarmagedoom · May 09, 2018 at 08:37 AM
Good day.
You have the OnTriggerStay() funtion, which will be executed when a collider is inside the trigger of the gameobject (every frame).
Thena good solution is to create a tag for OxigenGenerator Object fro example "OxiGen", and check if the collider that is inside have this tag.
void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "OxiGen")
{
Debug.Log ("Oxigen generator detected!!");
}
}
Byeee!
Hi @tormentoarmagedoom, Thanks for your reply.
Sadly it doesn't really work as I was hoping. In fact, it doesn't even register that the generator is inside the safezone. This probably is so because it is already placed in the safezone before spawn.
Any other ideas?
Thanks ;)