How to change animations for only 5 seconds?

Hi everyone.

I have an object that every time when I press it start the animation (flapping wings).

I would like to add a second animation: “YellowBoom”, which would replace the previous one only for 5 seconds after the collision with the object “GoldWorm”.

After 5 seconds, the animation should return to the original form.

How to change animations for only 5 seconds?

Below is my script that I wrote and I really don’t know what and where to add.

I would be grateful for help, because it took a little to write this script and I would not like to smash it :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBehaviour : MonoBehaviour {

    public Transform mesh;
    public float forceFly = 100;
    private Animator animatorPlayer;

    private float currentTimeToAnim;
    private bool inAnim;

    // Use this for initialization
    void Start () {
        animatorPlayer = mesh.GetComponent<Animator>();

        this.gameObject.GetComponent<Rigidbody2D> ();
    }
   
    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown (0))
        {
            GetComponent<AudioSource>().Play();
           
            inAnim = true;

            GetComponent<Rigidbody2D> ().velocity = Vector2.zero;
            GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, -1) * forceFly);

        }

        if (inAnim) {
            currentTimeToAnim += Time.deltaTime;

            if (currentTimeToAnim > 0.4f) {
                currentTimeToAnim = 0;
                inAnim = false;

            }
        }

        animatorPlayer.SetBool ("callFly", inAnim);
    }
        void OnCollisionEnter2D(Collision2D coll){
        if (coll.gameObject.tag == ("Enemy"))
            Destroy (gameObject);
        {
           
            if (coll.gameObject.tag == ("GoldWorm")) {
                forceFly = 250;
                StartCoroutine (FiveSecondsCooldown ());

            }
            else {
                    if (coll.gameObject.tag == ("Hook")) {
                        forceFly = 5;
                        StartCoroutine (FiveSecondsCoolUp ());
                    } 
                }
            }
        }


    public IEnumerator FiveSecondsCooldown() {
        forceFly = 250;
        yield return new WaitForSeconds(5f); //
        forceFly = 100;
    }

    public IEnumerator FiveSecondsCoolUp() {
        forceFly = 5;
        yield return new WaitForSeconds(5f); //
        forceFly = 100;
}
}

Hi, I think an easy way to achieve what you want would be to create a new trigger in your animator controller to transition from the flying to the “Yellow Boom” then have that animation simply transition back to flying if the bool “call Fly” is true but set the exit time off the transition to 5 seconds

If I could suggest, as a general rule you may want to reconsider that sentiment. Rather, always look for ways to improve or do something better - even if that means throwing away some previous work. Sometimes fewer lines of code can be a superior solution to a problem. Plus it is generally easier to understand code when there are fewer lines to read.

This scenario may be a case in point. I would agree with N_Murray’s solution. Let the Animator Controller do the work for you; no need for coroutines. Keep it simple. :slight_smile: