- Home /
Help with trigger
I am working on a horror game and i have a section where i have a cube as a trigger, when the player enters the trigger a dead body appears and falls down using Instantiate, my only problem is everytime you leave the trigger and come back in, Instantiate is triggered again and another dead body appears and falls down ontop of the first one. How do i make it so Instantiate is only activated that first time and after even if the player walks through the trigger again Instantiate isnt activated again?
Answer by Sundar · Sep 28, 2012 at 12:16 AM
Use a single boolean to control dead body fall
bool bodyFellOnce = false;
void OnTriggerEnter()
{
if( !bodyFellOnce )
{
// your body instanciation
bodyFellOnce = true;
}
}
so here is my code where do i add yours in at?
var pos : Transform;
var deadGuy : GameObject; //a prefab of the dead guy
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "Player")
Instantiate(deadGuy, pos.position, pos.rotation);
}
Here you go...
var pos : Transform; var deadGuy : GameObject; //a prefab of the dead guy var
bodyFellOnce = false;
function OnTriggerEnter(other : Collider){
if( !bodyFellOnce && ( other.gameObject.tag == "Player" ) )
Instantiate(deadGuy, pos.position, pos.rotation);
bodyFellOnce = true;
}
its not working it gives me an error sayig bodyFellOnce is an unknown identifier
nvm i got it to work! thanks so much! now one more thing i have a cylinder that is a trigger for audio too can i make it play the sound once too?
yes @TwistedDead it should work fine with audio, what this script does is it keeps track if you hiy the certain trigger if you did it sets a value and if you hit the trigger again nothing would happen because the value has been set and you said that it should only work if it has been set. If you know what I mean xD. So whatever you do in the OnTriggerEnter + the value(like shown above) its' s ok!