- Home /
change animator parameters with the input system
Hello Everyone. I have a character's animator with a simple animation tree. Transitions between animation states are using boolean variables. I just want to know how to change these boolean variables when I input a button. For example, when pressing "W" on the keyboard the "isWalking" becomes true.
there is the setbool property of the animator. you just need to reference the animator in your script and call
anim.SetBool("isWalking", Input.GetKey(KeyCode.W));
or just change the input part with true or false or any other boolean when you wanna set it
Answer by Robotinker · Aug 27, 2021 at 07:05 PM
Write a MonoBehaviour that listens for input and sets the parameter value and attach it to your game object. Something like...
using UnityEngine;
public class MovementAnimationListener : MonoBehaviour
{
public Animator MyAnimator;
void Awake()
{
MyAnimator = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
MyAnimator?.SetBool("isWalking", true);
}
else if (Input.GetKeyUp(KeyCode.W))
{
MyAnimator?.SetBool("isWalking", false);
}
}
}
Thansk Robotinker for your answer. but when I tried the code you gave, I got this error after running the game.
InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings. UnityEngine.Input.GetKeyDown (UnityEngine.KeyCode key) (at :0) CharAnimeControl.Update () (at Assets/Scriptz/CharAnimeControl.cs:22)
I'm using the new input system. and already included using UnityEngine.InputSystem;
did I miss something here?
Have you set up a control scheme using the new input system? https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide.html
Once you do that, you'll be able to put a function in your component that responds to an event name like "OnMove". For example:
public void OnMove(InputValue value)
{
if(value.Get<Vector2>().magnitude > 0)
{
MyAnimator?.SetBool("isWalking", true);
}
}