Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

animation plays when button is pressed, but when I let go it won't play again

Discussion in 'Animation' started by tomtricoire, Jul 18, 2020.

  1. tomtricoire

    tomtricoire

    Joined:
    Apr 4, 2020
    Posts:
    3
    I have created a script so that my animation starts when I press 'd' and stops when I let go. But then it won't start again (when I press d).

    using UnityEngine;

    public class animation_hero : MonoBehaviour
    {
    public GameObject hero;

    void Update()
    {
    if (Input.GetKey("d"))
    {

    hero.GetComponent<Animator>().Play("animation");

    }

    if (Input.GetKeyUp("d"))
    {
    hero.GetComponent<Animator>().enabled = false;

    }

    }

    }
     
  2. tomtricoire

    tomtricoire

    Joined:
    Apr 4, 2020
    Posts:
    3
    Fixed it, just had to enable the animator when I press d