- Home /
How can I access Animator parameters in C#
I'm fairly new at C# in Unity and I'll like to access a parameter in my player Animator controller in my C# script. I want to make an IF statement saying: if my animator float parameter is greater then 0.5 then say something in the console. But I don't know how to write this in C#. Can someone please help me out?
Answer by JustinMeyer · Aug 22, 2017 at 03:32 PM
Thx I figured it out. I just wrote: if(anim.GetFloat ("speedPercent") > 0.5){ Debug.Log("running"); }
anim is a variable for my Animator. I'm pretty sure that I'v already tried this code before so I dont know why its working now. Thx again for the replies.
Answer by x4637x · Aug 22, 2017 at 12:21 PM
Animator myAnimator;
//using variable name
myAnimator.GetFloat("[variable name here]");
//using variable index
myAnimator.GetFloat(0);
The float parameter I want to access is called: speedPercent I wright if(anim.GetFloat("[speedPercent]") == 0.5) { Debug.Log("running"); } And it gives me this error Parameter '[speedPercent]' does not exist.
you should write the name without the brackets "[" "]"
Note that it's not the index but the hash of the name that you provide to the second overload. You get the hash using Animator.StringToHash
. The overload is provided as a performance optimization; if you use a parameter name a lot of times, it's faster to hash it once and then use the hash for Get* and Set* methods.
Sorry , by "variable index" I mean the id number in the animator "parameters" tab, which is the same thing you get by doing Animator.StringToHash
. If you already know the id number form there, you could simply use it like: First parameter name "speedPercent", myAnimator.GetFloat(0);
is the same as myAnimator.GetFloat("speedPercent");
Your answer
Follow this Question
Related Questions
Animator subtracting float to change states in c#? 0 Answers
Multiple Cars not working 1 Answer
Can you help solve the following error? 1 Answer
Distribute terrain in zones 3 Answers