Search Unity

how can i do to stop an animation after 2 seconds ("2f") in an OnTriggerExit?

Discussion in 'Scripting' started by Nixel2013, Aug 1, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    public class PinchoSimple : MonoBehaviour
    {
    public Animator Pincho;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnTriggerEnter(Collider pincho)
    {
    if (pincho.CompareTag("Player"))
    {
    Pincho.enabled = true;
    }
    }

    void OnTriggerExit(Collider pincho)
    {
    if (pincho.CompareTag("Player"))
    {
    Pincho.enabled = false;
    }
    }

    }
     
  2. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    You can either start a coroutine that waits two seconds before stopping the anim, e.g.

    iEnumerator StopAfterTwoSeconds()
    {
    yield return new WaitForSeconds(2f);
    StopMyAnim();
    }

    , or call a method that stops the anim with Invoke, e.g.:

    Invoke("StopMyAnim", 2f);

    -Scott