Search Unity

Moving Animation won't execute

Discussion in 'Getting Started' started by Gabrieldonley, Jan 11, 2016.

  1. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    I'm currently working on the survival shooter tutorial, and so far, I can move and do all that good stuff, but when I move forward, back, left, or right, my moving animation does not execute. The idling anim does, though. Here is my script if that helps. Any suggestions?
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 6f;
    6.  
    7.     Vector3 movement;
    8.     Animator anim;
    9.     Rigidbody playerRigidbody;
    10.     int floorMask;
    11.     float camRayLength = 100f;
    12.  
    13.     void Awake()
    14.     {
    15.         floorMask = LayerMask.GetMask ("Floor");
    16.         anim = GetComponent <Animator> ();
    17.         playerRigidbody = GetComponent<Rigidbody> ();
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         float h = Input.GetAxisRaw ("Horizontal");
    23.         float v = Input.GetAxisRaw ("Vertical");
    24.  
    25.         Move (h, v);
    26.         Turning ();
    27.         Animating (h, v);
    28.     }
    29.  
    30.     void Move (float h, float v)
    31.     {
    32.         movement.Set (h, 0f, v);
    33.  
    34.         movement = movement.normalized * speed * Time.deltaTime;
    35.  
    36.         playerRigidbody.MovePosition (transform.position + movement);
    37.     }
    38.  
    39.     void Turning ()
    40.     {
    41.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    42.  
    43.         RaycastHit floorHit;
    44.  
    45.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    46.         {
    47.             Vector3 playerToMouse = floorHit.point - transform.position;
    48.             playerToMouse.y = 0f;
    49.  
    50.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    51.             playerRigidbody.MoveRotation (newRotation);
    52.         }
    53.     }
    54.  
    55.     void Animating(float h, float v)
    56.     {
    57.         bool walking = h != 0f || v != 0f;
    58.         anim.SetBool ("IsWalking", walking);
    59.     }
    60. }
    61.  
    62.  
    63.  
    64.  
    65.  
    66.  
    67.  
    68.  
    69.  
    70.  
    71.  
    72.  
    73.  
    74.  
    75.  
    76.  
    77.  
    78.  
    79.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm guessing the problem is most likely not in the code, but in your Animator Controller. Please double-check that the "IsWalking" bool in that state machine actually causes a transition to a walking animation. In fact you can test that separate from the script by just running the game, disabling the above script, and then tweaking the IsWalking parameter in the animator controller manually.
     
  3. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    Never mind...I figured it out. Have to uncheck has exit time in the new U. 5 state machine
     
    JoeStrout likes this.