Home > Enterprise >  Example third person controler in Unity / StringToHash
Example third person controler in Unity / StringToHash

Time:07-18

I am trying to learn game development through different ways and sources and recently I have created a project in unity with example ThirdPersonController.

While looking through the code I came across one thing which I am finding difficult to wrap my mind around. It is "StringToHash". What is the purpose of this? Here is an example of variable with specified values set:

  private void AssignAnimationIDs()
{
            _animIDSpeed = Animator.StringToHash("Speed");
            _animIDGrounded = Animator.StringToHash("Grounded");
            _animIDJump = Animator.StringToHash("Jump");
            _animIDFreeFall = Animator.StringToHash("FreeFall");
            _animIDMotionSpeed = Animator.StringToHash("MotionSpeed");
}

and here is an example when this is used when being called:

  if (_hasAnimator)
{
                _animator.SetFloat(_animIDSpeed, _animationBlend);
                _animator.SetFloat(_animIDMotionSpeed, inputMagnitude);
}

CodePudding user response:

Welcome to Stack overflow. Similar question already has been asked HERE . Unity Docs .

In your code below you changing values (parameters) of your animator. To find which values it should change it must compare the name of the value you gave in .SetFloat to the ones it has (in "parameters" section on "animator" tab).

You could do .SetFloat("speed", _animationBlend);

But to get that milliseconds of time advantage (optimization) your "speed" is converted to integers first (like in your code above) and then you use it.

  • Related