Search Unity

Animator Not Picking Up Boolean Change

Discussion in 'Animation' started by samdaman93, Jul 11, 2018.

  1. samdaman93

    samdaman93

    Joined:
    Oct 3, 2015
    Posts:
    37
    Hello, Beginner here. I seem to be having a problem with the animator. All the other booleans are registering changes fine but the isFiring boolean doesn't seem to register in the animator. I have tried making it public to see whether it changes in the inspector while the game is running and it does, however the animator doesnt register it as changed.

    Any thoughts? Code posted below, most of it is the movement part of the script. The bottom deals with the firing part.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class controller : MonoBehaviour {
    5.  
    6.     //Character controller variables
    7.     public float speed = 6.0F;
    8.     public float jumpSpeed = 8.0F;
    9.     public float gravity = 30f;
    10.     public float speedacceleration = 0.5f;
    11.     private Vector3 moveDirection = Vector3.zero;
    12.     private CharacterController Charcontroller;
    13.  
    14.     //Head look variables
    15.     private Transform HeadTransform;
    16.     private float rotY;
    17.     private float rotZ = 0;
    18.     public float HeadZMin;
    19.     public float HeadZMax;
    20.     public float mousensensitivity;
    21.  
    22.     //Body Movement variables
    23.     private Transform BodyTransform;
    24.     public float Bodyrotspeed = 0.05f;
    25.     private float headTempRotation;
    26.     private float bodyTempRotation;
    27.     public float bodyrotatemaxspeed = 10f;
    28.     public float diagWalk;
    29.  
    30.     //animations
    31.     private Animator animator;
    32.  
    33.     //Fighting Variables
    34.     private bool isFighting;
    35.     public bool isFiring;
    36.     private float fightTimer;
    37.     public float fightCount = 10f;
    38.  
    39.     void Start()
    40.     {
    41.        animator = gameObject.GetComponent<Animator>();
    42.        HeadTransform = transform.Find("Head");
    43.        Charcontroller = gameObject.GetComponent<CharacterController>();
    44.        BodyTransform = transform.Find("Torso_Pivot");
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.         //Walking
    50.         //Head Rotation
    51.         rotY = Input.GetAxis("Mouse X")*mousensensitivity;
    52.         HeadTransform.transform.Rotate(0, rotY, 0);
    53.  
    54.         rotZ += Input.GetAxis("Mouse Y");
    55.         rotZ = Mathf.Clamp(rotZ, HeadZMin, HeadZMax);
    56.         HeadTransform.transform.localEulerAngles = new Vector3(0, HeadTransform.transform.localEulerAngles.y, rotZ);
    57.  
    58.         //Movement
    59.  
    60.         if (Charcontroller.isGrounded)
    61.         {
    62.             moveDirection = new Vector3(Input.GetAxis("Vertical"), 0, -Input.GetAxis("Horizontal"));
    63.             moveDirection = Quaternion.Euler(new Vector3(0,HeadTransform.eulerAngles.y,0)) * moveDirection;
    64.             moveDirection = transform.TransformDirection(moveDirection);
    65.             moveDirection *= speed;
    66.             if (Input.GetAxis("Vertical") != 0)
    67.             {
    68.                 animator.SetBool("isWalking", true);
    69.                 if (Input.GetAxis("Vertical") < 0)
    70.                 {
    71.                     animator.SetBool("isWalkingBackwards", true);
    72.                 }
    73.             }
    74.             else
    75.             {
    76.                 animator.SetBool("isWalking", false);
    77.             }
    78.  
    79.             if (Input.GetAxisRaw("Vertical") == 0 & Input.GetAxisRaw("Horizontal") != 0)
    80.             {
    81.                 animator.SetBool("isWalkingRightOnly",true);
    82.                 if (Input.GetAxis("Horizontal")<0)
    83.                 {
    84.                     animator.SetBool("IsWalkingLeftOnly", true);
    85.                 }
    86.             }
    87.             else
    88.             {
    89.                 animator.SetBool("isWalkingRightOnly", false);
    90.             }
    91.  
    92.             if (Input.GetButton("Jump"))
    93.                 moveDirection.y = jumpSpeed;
    94.             //Body Rotation only possible when grounded
    95.             if (Input.GetAxisRaw("Vertical") != 0 & Input.GetAxisRaw("Horizontal") != 0)
    96.             {
    97.                 if (Input.GetAxis("Vertical") < 0){
    98.                     diagWalk = -45 * Input.GetAxis("Horizontal");
    99.                 }
    100.                 else
    101.                 {
    102.                     diagWalk = 45 * Input.GetAxis("Horizontal");
    103.                 }
    104.             }
    105.         }
    106.  
    107.  
    108.         moveDirection.y -= gravity * Time.deltaTime;
    109.         Charcontroller.Move(moveDirection * Time.deltaTime);
    110.  
    111.         //Rotation
    112.  
    113.         if (Mathf.Abs(rotY) > bodyrotatemaxspeed)
    114.         {
    115.             BodyTransform.Rotate(0, rotY, 0);
    116.         }
    117.  
    118.             BodyTransform.rotation = Quaternion.Lerp(BodyTransform.rotation, Quaternion.Euler(BodyTransform.localEulerAngles.x,HeadTransform.localEulerAngles.y+diagWalk,BodyTransform.localEulerAngles.z),Bodyrotspeed);
    119.  
    120.         diagWalk = 0;
    121.  
    122.         //Fighting
    123.         //Ready Weapon
    124.         if (Input.GetMouseButtonDown(0) && !isFighting)
    125.         {
    126.             isFighting = true;
    127.             fightTimer = fightCount;
    128.             animator.SetBool("isFighting", true);
    129.         }
    130.  
    131.         fightTimer -= Time.deltaTime;
    132.  
    133.         //Unready Weapon timer
    134.         if (fightTimer <= 0)                            
    135.         {
    136.             isFighting = false;
    137.             animator.SetBool("isFighting", false);
    138.         }
    139.  
    140.         //Fire
    141.         if (Input.GetMouseButtonDown(0) && isFighting)
    142.         {
    143.             Debug.Log("fire!");
    144.             fightTimer = fightCount;
    145.             isFiring = true;
    146.         }
    147.  
    148.     }
    149.  
    150.     //Animation Event Function
    151.     void isNotFiring()
    152.     {
    153.         isFiring = false;
    154.     }
    155. }
     
  2. LanslowDuLac

    LanslowDuLac

    Joined:
    Jun 4, 2018
    Posts:
    18
    Hi,
    See if isFiring value is changing in one update after passing a new value. I didn't read your code but if your animator is not considering your changes in a parameter, that's maybe because you're changing it again after one frame update.
    So check your conditions and isFiring changes. :)
     
  3. samdaman93

    samdaman93

    Joined:
    Oct 3, 2015
    Posts:
    37
    Is firing works when I set it to public and view it in the inspector. I'm not sure why the animator isn't picking up the condition change?