Search Unity

Animation on Mouse Button

Discussion in 'Animation' started by heytheremogwai, May 26, 2019.

  1. heytheremogwai

    heytheremogwai

    Joined:
    Mar 22, 2018
    Posts:
    36
    Hi,

    I'm working on a project just as a hobby here, learning as I go along.. I'm having trouble figuring out the best way to control animations (during movement). When I hold both mouse buttons down, I'm able to move but the "run" animation starts and freezes in the first frame. From what I understand here, anim.Play() is resetting over and over, instead of playing out but I can't figure out how to do this another way.

    Code (CSharp):
    1.     void Update(){
    2.        
    3.         anim = GetComponent<Animator>();
    4.         anim.Play("idle", 0, 0f);
    5.         if(Input.GetMouseButton(0) && Input.GetMouseButton(1)){
    6.             SetTargetPosition();
    7.             Move();
    8.             anim.Play("run", 0, 0f);
    9.         }
    10.     }