- Home /
box collider trigger question
ok so i kinda have 2 questions. ive been able to figure out how triggers work with box colliders and send messages and look at all that fun stuff. but now the hard parts. 1st question im wondering if its possible to have a collider trigger after walking across it like 2 times? for example i want to be able to walk across it once with nothing happening, then when u walk over it again thats when it triggers whatever. next question is is it possible to make them appear and dissapear at certain times. another example being like i have a level that requires you to collect keys. when i pick up the first key i want to be able to delete all those box colliders i had before and then make a whole new set of them. then again for the second key and so on. i hope i made sense in all that, if not ill try to explain it more.
sorry if its kinda long :(
Answer by GC1983 · Sep 05, 2013 at 06:44 AM
Question 1: Create an int count so it will up the count each time you walk over the collider. If the desired count is met, trigger the effect.
Question 2: Im not sure what you mean by deleting all the box colliders from before. But, instead of deleting the colliders, just turn the activate/deactivate through object.renderer.enabled = true/false.
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Tag Name")
{
other.renderer.enabled = true;
}
}
can u give a example of how in count works? im still a noob with scripting. for the second question that might work. but just to explain what i mean is like i have a house and you have to get keys to escape with a ghost trying to kill you. now as u get more keys i want the ghost to be more hostile and appear more often. now my first thought would be to add more colliders after every key but get rid of the ones from the key before so u dont trigger those ones. or so you wont trigger all of them before u get all the keys. does that make more sense?
int count = 0;
void OnTriggerEnter(Collider other)
{
count++;
if(count >= 3)
{
//do something
}
}
As for the keys, just place them around the scene and give them a collider and set them to isTrigger so they wont be in the way and will trigger if you collide. In the OnTriggerEnter method for the keys, have them update the ghost aggression and mark you have possession of the key so you can unlock whatever door it belongs to, then just simply delete or hide the key you collided with. You can call whatever in the OnTriggerEnter or have a method called within it. $$anonymous$$ake sense?
I think you're getting your terms mixed up $$anonymous$$wayzie. Colliders are simply the components that trigger collision messages. The key itself isn't the collider.
As in GC's answer, the count is the way to track the number of times a collisions has occurred, but the collider will fire every time, until you turn it off. But it's the count that then controls when your object's behaviour changes.
As for the second part - if you have a key prefab with a collider attached, you can easily create and destroy your keys in the scene. The destroy can happen when the collider triggers, and you can remove the key from the scene (look at gameObject.Destroy in the docs) and as for adding more keys, if you are using a prefab you can use Instantiate to create a game object in a particular position in your scene (look at GameObject.Instantiate in the docs).
thanks GC thats what i was looking for int count wise :) but i still dont think yall understand me about the other one lol i really suck at explain things sorry :( how about this...hmmm i have my lock system in place and all my keys set up for different doors, destroying the keys when picked up so all that works just fine. but lets say when i first begin the game i want there to be no box colliders around the house, then once i grab lets say "key1" i want lets say "group 1" of the box colliders to be active which are spread through out the house so you can trigger them when walked across. then once you grab "key 2" i want "group 1" of colliders to go away so you cant trigger them any more and then make "group 2" active or show up....i really hope that explanation is better this time.