Controlling Animations Through C# With The Unity Animator
Bridging the gap between Unity’s built-in animation system and your custom code is pretty simple, but may be daunting when learning for the first time. I’ll try and get you started with the basics.
Today‘s Objective: Show you how to quickly and easily connect with and activate animations from code.
Current setup:
The Trigger type is basically a Boolean, but it automatically sets itself to false after 1 frame. So it triggers animations, but doesn’t maintain them.
Accessing through code:
I have a Vector2 variable “_keyboardInput” which is set to the current movement input from the keyboard, normalized. To send the X movement input to the “VelocityX”, I call the function “SetFloat()” from the Update() method.
“SetFloat()” is used because the “VelocityX” parameter is of the type “float”. For other parameter types, equivalent functions exist:
For the booleans that control which way the Player’s ship tilts, I use this code. (I do think this could be designed a little better, but haven’t got around to doing anything about it yet)