Home > Back-end >  How to change the float value in a script?
How to change the float value in a script?

Time:02-14

Hello im trying to figure out how to change this float value within a script to trigger a animation any tips on how to?

Image to help

CodePudding user response:

void Start()
    {
    m_Animator = gameObject.GetComponent<Animator>();
  }

   void YourFunction()
     {
    m_Animator.SetFloat("Speed", YourValueAsAVariable);
     }

CodePudding user response:

You should use method Animator.SetFloat

More about Animator.SetFloat here https://docs.unity3d.com/ScriptReference/Animator.SetFloat.html

Here is an example from the mentioned link

Animator m_Animator;
float m_HorizontalMovement;

void Start()
{
    //Get the animator, which you attach to the GameObject you are intending to animate.
    m_Animator = gameObject.GetComponent<Animator>();
}

void Update()
{
    //Translate the left and right button presses or the horizontal joystick movements to a float
    m_HorizontalMovement = Input.GetAxis("Horizontal");
    //Sends the value from the horizontal axis input to the animator. Change the settings in the
    //Animator to define when the character is walking or running
    m_Animator.SetFloat("horizontalSpeed", m_HorizontalMovement);
}
  • Related