Search Unity

Animation not playing when player moves

Discussion in 'Animation' started by CoffeeConundrum, Jan 15, 2015.

  1. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Hi guys,
    Been trying to get a simple walking animation to play when the player moves around the screen.
    Below is the gameobjects attached components inc. an animation and also the script that I've got attached to the player.


    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         HandleInput();
    4.     }
    5.  
    6.     void HandleInput()
    7.     {
    8.         if (Input.GetKey(KeyCode.W))
    9.         {
    10.             yPos += 0.1f;
    11.             animation.Play("WalkingPlayer");
    12.         }
    13.         if (Input.GetKey(KeyCode.S))
    14.         {
    15.             yPos -= 0.1f;
    16.             animation.Play("WalkingPlayer");
    17.         }
    18.         if (Input.GetKey(KeyCode.A))
    19.         {
    20.             xPos -= 0.1f;
    21.             animation.Play("WalkingPlayer");
    22.         }
    23.         if (Input.GetKey(KeyCode.D))
    24.         {
    25.             xPos+=0.1f;
    26.             animation.Play("WalkingPlayer");
    27.         }
    28.  
    29.         CheckPosition();
    30.  
    31.         newPos = new Vector3(xPos, yPos, 0);
    32.         gameObject.transform.position = newPos;
    33.        
    34.     }
    35.  
    I've had a look online and can't see any solutions that apply to my issue. Any help would be greatly appreciated!
     

    Attached Files: