Plant moving on trigger enter

I have this code that makes plants move as the player walks past them (like they just jiggle a little) but for some reason it only works once and then when the player walks past them again, nothing happens… is it something in my code?

    private Animator anim;
    private IEnumerator coroutine;

    void Start()
    {
        anim = GetComponent<Animator>();
        coroutine = Wobble();
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            StartCoroutine(coroutine);
        }
    }

    IEnumerator Wobble()
    {
        anim.SetBool("move", true);
        yield return new WaitForSeconds(0.5f);
        anim.SetBool("move", false);
        yield return new WaitForSeconds(0f);
    }

Just setup your coroutine in the OnTriggerEnter2D as :

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            coroutine = Wobble();
            StartCoroutine(coroutine);
        }
    }

It should work

1 Like

Oh my god, I had no idea… Thank you!!