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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Animator bool parameters enable

Discussion in 'Scripting' started by MikeyJY, Mar 13, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    That's 2 parameters that's related to this error:
    Flintlock, FlintlockRightCick;
    When Flintock = true, the transition between idle and Flintlock Hold animation is enabled. When FlintlockRightClick = true, the transition between Flintlock Hold and FlintlockRightClick(Aim) is enabled.
    As you can see in the video, it works ok if I enable manually the parameters.
    I want to make the the bool FlintlockRightClick to enable on right click pressed:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Inventory.GetComponent<InventoryItems>().ItemInHand.tag == "Flintlock") //check if the player has the flintlock in hand
    4.         {
    5.             if (Input.GetKey(KeyCode.Mouse1))
    6.             {
    7.              
    8.              
    9.                 GetComponent<Animator>().SetBool("FlintlockRightClick", true);
    10.  
    11.             }
    12.             else
    13.             {
    14.                 GetComponent<Animator>().SetBool("FlintlockRightClick", false);
    15.              
    16.             }
    17.         }
    18.  
    19.     }
    In the next video I pressed RightClick and the bool doesn't activate:


    No error in console.
    I checked if Input.GetKey(KeyCode.Mouse1) is working and it is.
    The issue is at this line GetComponent<Animator>().SetBool("FlintlockRightClick", true);
    I tried:
    if (Input.GetKey(KeyCode.Mouse1))
    {

    click = true;
    GetComponent<Animator>().SetBool("FlintlockRightClick", true);

    }

    and the click is true when I press RightClick.
    If I change the order:
    if (Input.GetKey(KeyCode.Mouse1))
    {


    GetComponent<Animator>().SetBool("FlintlockRightClick", true);
    click = true;
    }

    Click is not enabled.
     
    Last edited: Mar 13, 2020
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    So you turn the bool on when you press right-click, and you turn the bool off when you release right-click. You animator transition "has exit time" is set true, which means the animator will only check your bool when the current animation has finished and restarts it's loop. This means you will only transition animation states if you are holding the right mouse button when the current animation restarts. (You can even see in the video after you set the bool manually, it doesn't move to the next animation until the current animation finishes)


    If that doesn't work, try adding a Debug.Log() and log out
    (Inventory.GetComponent<InventoryItems>().ItemInHand.tag == "Flintlock").ToString()
    so you can see in your console if your script can even tell when you're holding the Flintlock.
     
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Thanks, that was the problem at the checking of the ItemInHand. I forgot to assign the inventory gameObject in the Inspector.