Previously: How Components and Scripts works
HowÂ Animations and AnimationControllers works
Now we know about components and scripts let us try out unity’s built-in animation features. While some games only make use of scripted animation you most likely want to use more traditional keyframed animation.
Unity’s animation system is extremely powerful since it allows you to key any parameter in Game Objects, including variables from your custom code. You are not limited to animating the visuals but can use the system for game mechanics as well.
In our example we will use it to extend and retract our plane’s landing gear. For this we will need an Animation Controller to control which animation is playing and 2 Animation Clips. Right click on your AnimationControllers directory in the Project and select Create > Animation Controller, name it AirplaneAnimationController, likewise create 2 Animation files named AirplaneExtentendedLandinggear andÂ AirplaneRetractedLandinggear.
We won’t be animating the actual movement in this example, but rather use the clips to store our key poses and then let our animation controller blend between the poses. First we will need to add the Animation Controller to our Airplane, just drag and drop it from Projects to Inspector.
Now we are ready to set up Animations files in our Animation Controller. Open the Animator Window.
The animation controller is a state machine.Â A state machine is a network of states with transitions between possible state changes. State Machines are used for a lot of different utilities beyond animation, a common one in games being nonplayer character AI, since the Animator state machine allows you to key all parameters of GameObjects you can use it for this as well, but we won’t be doing something this advanced today. In out example we want to use our extended and retracted Animation files as states. So let us drag and drop them from the Projects to the Animator Editor.
To create transitions between states right click the source select Make Transition and click on the destination. In our example we want to connect both the extended to the retreacted state and the retracted to the extended state.
A state machine can only be in one state at any given time but can change state when given some input. The inputs that controls the Animation Controller are stored as parameters. In our case we want to use a Bool, which is a True/False statement. To create a Parameter go to the Parameters tap and click the plus. Create that bool and name it as a statement that can be True or False so we do not confuse ourselves.Â
We want our state machine to go from extended to retracted if the bool is false and from retracted to extended if it is true. To set this up select the transition line and in the Inspector add the LandingGear IsExtended Parameter to the conditions at the very bottom. Again we need to do this for both transitions.
While we are here we want to set up our transition blend. We want to uncheck Has Exit Time, this option prevents the state machine from transitioning until its animation has finished. We also want to set the Transition Duration.
Now all there is left to do is to put our key poses inside our Animations. If you open the Animation window and select our Game Obejct with the Animation Controller component you will notice that our Animations are already linked.
If you do not see your Animations here your states in the Animation Controller does not have motions assigned to them. Just select the state and assign the motion in the Inspector.
In our example we only want to key the Rotation of the Landinggears. Let us add these Properties to both Animations.
We can leave the extended Animation in the default pose and rotate the gear up in the retracted Animation.
Now save the project. Since Animations and Animation Controllers settings are stored in files outside the scene we need to save our project. Until the project is saved unity stores all our work in temporary files which cannot be shared with version control such as GIT.
All done! Press play and you can now retract and extend the gears by toggleing the Parameter in the Animation Controller.
Next up: How GIT and Version Control works