Hi, i’m trying to write up a script that plays an animation when a key is pressed on the screen, and immediately after moves the camera to another location. but i’ve encountered a problem where the animation will play after the key is pressed and the camera will move at the same time so before the animation even got through the first frame i no longer see it. is there a type of delay or wait method that can allow me to wait 1 second after so i can make the animation visible before moving the camera?
below is the function i’ve set up for it.
public void Anime(){
Instantiate(explosion, touchpos, Quaternion.identity); //explosion animation
hit.transform.SendMessage("HideMe"); //disable renderer for object
Instantiate(shatter, hit.transform.position, Quaternion.identity); //shattering animation of object
//delay script here ??
MoveCamera();
hit.transform.SendMessage("ShowMe"); //re-enable render for object
}
Make it a Coroutine, and then use WaitForSeconds as described in that doc page. Note in C# you’ll need to call this function using StartCoroutine(Anime()) instead of a regular function call.
for anyone in the future that come across this:
my problem was that i assumed that i can have the below script as a function i can call up whenever i needed to have a 1 second delay in my coding.
IEnumerator WaitOneSecond() {
yield return new WaitForSeconds(1);
}
like something like this was what i assumed it to be
void Update() {
//do some stuff
StartCoroutine(WaitOneSecond()); //delay for a second
//continue do some other stuff
}
But this is incorrect. in the end i had something like this that worked