GetButton not working

I tried using Input.GetButtonDown for my animation but instead of playing the animation it just stutters…

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

public class PlayerController : MonoBehaviour
{
    public GameObject thePlayer;
    public float horizontalMove;
    public float verticalMove; 
    public int horizontalMoveSpeed;
    public int verticalMoveSpeed;
    void Update()
    {
        if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
        {
         thePlayer.GetComponent<Animation>().Play("Walk");
         horizontalMove = Input.GetAxis("Horizontal") * Time.deltaTime * horizontalMoveSpeed;
         verticalMove = Input.GetAxis("Vertical") * Time.deltaTime * verticalMoveSpeed;
         transform.Rotate(0, horizontalMove, 0);
         transform.Translate(0, 0, verticalMove);
        }

        else
        {
         thePlayer.GetComponent<Animation>().Play("Idle");
        }
   
        if (Input.GetButtonDown("Fire2"))
        {
         thePlayer.GetComponent<Animation>().Play("StartAim");
        }
       
     }
}

this is the point of interest…

if (Input.GetButtonDown("Fire2"))
{
thePlayer.GetComponent<Animation>().Play("StartAim");
}

I’m not getting any errors…
I’m using legacy animations because it’s easier IMO…

GetButtonDown is only true for the first frame that the button is pressed. So your StartAim begins and then on the next Update, Idle is played instead.

Okay so what do i do?

But it still happens even when holding it

I tried GetButton and it still didn’t work…

You’re going to have to not play the Idle animation no-matter-what in your else statement to avoid it, either by switching to some state based logic or comparing the current playing animation before switching or something to that effect.

That’s one of the strengths of using Mecanim versus the old legacy system, it makes these sort of things easier by maintaining state for you.

I’m currently trying to convert it to mecanim but like i’m new to animator scripting