Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Character Running Animation not playing when I click left shift.

Discussion in 'Scripting' started by Yettie123, Jul 16, 2017.

  1. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    My character is able to walk normal and the run at a faster speed when I hold left shift with W but it doesn't trigger the running animation (the walking animation works fine), I have looked over my code for a while and I don't see where I went wrong. Any help is greatly appreciated.

    Here is my only code on the Character:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent (typeof(CharacterController))]
    6. public class FirstPersonController : MonoBehaviour {
    7.  
    8.     public float movementSpeed = 5.0f;
    9.     public float mouseSensitivty = 5.0f;
    10.     public float jumpSpeed = 20.0f;
    11.     public float runSpeed = 10.0f;
    12.  
    13.     float verticalRotation = 0;
    14.     public float upDownRange = 60.0f;
    15.  
    16.     float verticalVelocity = 0;
    17.  
    18.     CharacterController characterController;
    19.  
    20.     static Animator anim;
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         Screen.lockCursor = true;
    25.         characterController = GetComponent<CharacterController>();
    26.  
    27.         anim = GetComponent<Animator>();
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.         //Rotation
    33.  
    34.         float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivty;
    35.         transform.Rotate(0, rotLeftRight, 0);
    36.  
    37.  
    38.         verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivty;
    39.         verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
    40.         Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
    41.  
    42.  
    43.         //Movement
    44.  
    45.         float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
    46.         float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
    47.  
    48.         if (Input.GetKey(KeyCode.W) && Input.GetKey("left shift"))
    49.         {
    50.             forwardSpeed = runSpeed;
    51.             anim.SetBool("isIdle", false);
    52.             anim.SetBool("isWalking", false);
    53.             anim.SetBool("isRunning", true);
    54.         }
    55.         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) && !Input.GetKey("left shift"))
    56.         {
    57.             anim.SetBool("isIdle", false);
    58.             anim.SetBool("isWalking", true);
    59.             anim.SetBool("isRunning", false);
    60.         }
    61.         else
    62.         {
    63.             anim.SetBool("isIdle", true);
    64.             anim.SetBool("isWalking", false);
    65.             anim.SetBool("isRunning", false);
    66.         }
    67.  
    68.         verticalVelocity += Physics.gravity.y * Time.deltaTime;
    69.  
    70.         if(characterController.isGrounded && Input.GetButtonDown("Jump"))
    71.         {
    72.             verticalVelocity = jumpSpeed;
    73.         }
    74.  
    75.         Vector3 speed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed);
    76.  
    77.         speed = transform.rotation * speed;
    78.  
    79.         characterController.Move(speed * Time.deltaTime);
    80.  
    81.     }
    82. }
    83.  
     
  2. guillermoi

    guillermoi

    Joined:
    Sep 20, 2012
    Posts:
    27
    You may want to use triggers instead of booleans that way you don't have to worry about setting them to false.

    Also set the animator's parameters names in a constant variable in the class, this way it's less likely we mess up the names.

    In line 55 you may want to add parenthesis to separate the ORs from the AND.

    These may be source of problems, if you share a screenshot of the animator controller maybe we can see what's up.
     
  3. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    Set paranthesis in line 55 and change it to an else-if, else your code will hit both, line 48 and the else at 61 which sets isrunning at false again.
     
  4. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    Thanks roykoma and guillermoi it worked.
     
    guillermoi likes this.