Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Animator Not Picking Up Boolean Change

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

  1. samdaman93

    samdaman93

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

    unity_Y9-Qu4dmill45A

    Joined:
    Jun 4, 2018
    Posts:
    16
    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:
    10
    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?