Whenever my character runs, it has this weird loop, I asked other people and I checked tutorials. I could not find it. See video for more information.
Video:
Code:
public GameObject thePlayer;
public bool isRunning;
public float horizontalMove;
public float verticalMove;
void Update () {
if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
thePlayer.GetComponent<Animation>().Play("Run");
horizontalMove = Input.GetAxis("Horizontal") * Time.deltaTime * 150;
verticalMove = Input.GetAxis("Vertical") * Time.deltaTime * 8;
isRunning = true;
transform.Rotate(0, horizontalMove, 0);
transform.Translate(0, 0, verticalMove);
}
else
{
thePlayer.GetComponent<Animation>().Play("Idle");
isRunning = false;
}
}
The reason for that could be that you get the component each frame instead of just getting it once. That could cause problems. Try doing this:
void Start()
{
anim = thePlayer.GetComponent<Animator>();
}
and then play it in the Update function like this:
anim.Play("Run");
and then in your else statement, do the same with Idle just to make sure:
anim.Play("Idle");