For me the easiest way to accomplish this is with "Invoke".
http://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
Put whatever you want to be delayed into its own function and then invoke that function.
So let's say for example that I have this code ad I want the stuff in the if statement to happen on a five second delay:
if (playerHealth.currentHealth <= 0)
{
anim.SetTrigger ("GameOver");
died = true;
}
I would change it to this:
//I need a new variable. This is because I am trying to invoke something in an update script and I don't want to invoke it every single frame
public bool gameOver=false;
if (playerHealth.currentHealth <= 0)
{
//Check my new variable
if(gameOver==false)
{
//Invoke my GameOver function after 5 seconds.
Invoke("GameOver", 5.0f);
//Set the variable to true so it doesn't constantly invoke every frame
gameOver=true;
}
}
public void GameOver
{
//This is my function
anim.SetTrigger ("GameOver");
died = true;
}
Keep in mind I would at some point need to set that gameOver variable back to false. When the game restarts, perhaps.
↧