Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Checking animator floats etc in an 'if' statement

Discussion in 'Scripting' started by DustyShinigami, May 23, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I've figured out how to check animator bools in an 'if' statement by using GetBool, - if (anim.GetBool("isMoving")) - but for some reason, the same process isn't working for floats (GetFloat). How can this be done?

    Also, how can it be done with SetTrigger?
     
    Last edited: May 23, 2019
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    What trouble are you having with GetFloat?
     
  3. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    It just gives me an error no matter what I do. I can't specify the float for my Speed condition (0.2f when walking, 0f when not walking). It either tells me I can't convert type float to bool or that no overload for GetFloat takes two arguments.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    These error messages indicate that you have a syntax error in your code. It has nothing to do with GetFloat working or not. If you post the code maybe someone can point out the errors.

    If your getting an error message like "cannot convert 'float' to 'bool'" then you're probably doing something like this:
    Code (csharp):
    1. if (anim.GetFloat("Speed"))
    Remember that "if()" requires a Boolean expression in the parentheses in order to work. If you use a float in an "if" statement it's usually in the form of a comparison, like "if (Speed > 2f)" or something like that. "Speed > 2f" will either be true or false.
     
    DustyShinigami likes this.
  5. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Ahh. I see. I did try...

    Code (CSharp):
    1.     if (anim.GetFloat("Speed") == true/false)
    2.  
    ...or put an exclamation mark at the start if it was false, thinking that would work, but nada. Thanks though; I'll give that a try. :) How would you do it for checking triggers?
     
  6. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Hmm... I'm trying if (anim.GetFloat("Speed" > 2f)), but it doesn't appear to be working either. I've tried a couple of ways, such as removing the quotations, but nothing. :-\
     
  7. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    Code (csharp):
    1. if (anim.GetFloat("Speed") > 2f)
    Also, why not just check the float that is driving the animator's Speed variable?

    There must be some code like:
    Code (csharp):
    1. anim.SetFloat("Speed", myActualSpeedVariable);
    So just check myActualSpeedVariable instead.
    Code (csharp):
    1. if (myActualSpeedVariable > 2f)
     
    DustyShinigami likes this.
  8. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Excellent. Thank you. :D
     
  9. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Again, how can it, or is it possible, to do the same thing with SetTrigger?