Hi all, I want to add a delay to a ParticleEmitter that goes off when the player enters the trigger.
Im not sure how to go about it.
Heres the current code
var particles : ParticleEmitter;
function OnTriggerEnter (other : Collider){
if(other.gameObject.FindWithTag("Player")){
particles.emit = true;
} else{
particles.emit = false;
}
}
function OnTriggerExit (other : Collider){
if(other.gameObject.FindWithTag("Player")){
particles.emit = false;
} else{
particles.emit = false;
}
}
You can either use a coroutine with yield WaitForSeconds or you can set particles.emit = true;
in a separate method, which you can then call via Invoke with a delay.
First, your OnTriggerExit function has no difference between the true and false cases – you probably want to fix that.
Second, FindWithTag isn’t the normal way to do collision detection, normally you check the name like so:`
function OnCollisionEnter (col : Collision)
{
if(col.gameObject.name == "Player")
{
Destroy(col.gameObject);
}
}
I got these examples straight from the scripting reference. You should check there first, as it’s a really good resource.
These are the basic ideas; obviously change it to match your problem.
Do ();
print ("This is printed immediately");
function Do () {
print("Do now");
yield WaitForSeconds (2);
print("Do 2 seconds later");
}
Or if you want the code to wait until it’s finished executing:
yield StartCoroutine("Do");
print("Also after 2 seconds");
print ("This is after the Do coroutine has finished execution");
function Do () {
print("Do now");
yield WaitForSeconds (2);
print("Do 2 seconds later");
}
So for you, it’d be something like this:
var particles : ParticleEmitter;
var waitSeconds = 1;
function OnTriggerEnter (other : Collider){
yield WaitForSeconds (waitSeconds);
if(other.gameObject.FindWithTag("Player")){
particles.emit = true;
} else{
particles.emit = false;
}
}
function OnTriggerExit (other : Collider){
yield WaitForSeconds (waitSeconds);
if(other.gameObject.FindWithTag("Player")){
particles.emit = false;
} else{
particles.emit = true;
}
}
Note that I haven’t tested this code, as I normally program in C#.