Animation in Unity
Animations play an important role in game development, adding realism and dynamism to the interactive experiences. Unity provides tool for creating simpler to complex animations.
How to create animation
The detailed instructions below will guide you to create a simple scaling and descaling animation for a cube in Unity.
Note: Scaling refers to resizing an object along its x, y, and z axes.
Set up the project
Create a new 3D project .
Right click in the hierarchy view.
Create a 3D cube
GameObject.
Add animation and animator component
Select the cube and click on the Add Component in the inspector pane.
Search for Animation and add it as a component to the cube.
Search for Animator and add it as a component to the cube .
Note: To control the animation, make sure to uncheck the Play Automatically property in the Animation component attached to the cube.
Create animation
Select the
GameObject.Click Window.
Go to Animation > Animation.
Click on Create to create the animation (this will add animation and animation controller to the assets).
Click on the Add Property button.
Go to Transform > Scale >
Set the keyframes on the timeline by adjusting the cube's scale values at specific times.
Create multiple animations
Select the
GameObject.Open Window > Animation > Animation
Click on "Create new clip" to create another animation (to descale the cube to its original size).
Set animator
Click on the
GameObject.Drag and drop the animation controller to the Controller tab in the Animator component.
Go to Window > Animation > Animator. This will open the blend tree graph.
Note:
You can see the animation added to the animator (represented as a flow chart).
You can add the parameters, create states, or change the transitions here to control the animation.
Create a new parameter named
Scale 0of typeBoolto scale the cube.Create another parameter named
Scale 1of typeBoolto descale the cube.
Note:
Setting the parameter type to
Triggerwill play the animation automatically when the scene is rendered.To control the animation, you can use the
Boolparameter type.
Click on the arrow pointing to the animation in the animator window.
Add the
Scale 0parameter in the conditions tab (trigger is automatically reset after it has been used but theboolparameter has to be set to false after the transition is finished).Adjust the
Scale 1parameter accordingly to make a smooth transition from scaling the cube to descaling it.
Create a C# script
Create a new
C#script.Assign the script to the
GameObject.The script will call the
Scale 0parameter when a certain event happens, such as pressing a specific key.
A sample C# script is given below.
using System.Collections;using System.Collections.Generic;using UnityEngine;public class TriggerAnimation : MonoBehaviour{private Animator animator;// Use for initializationvoid Start(){animator = GetComponent<Animator>();}// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.P)){if(!animator.GetBool("Scale 0")){animator.SetBool("Scale 0", true)animator.SetBool("Scale 1", false)}}if (Input.GetKeyDown(KeyCode.S)){if(!animator.GetBool("Scale 1")){animator.SetBool("Scale 0", false)animator.SetBool("Scale 1", true)}}}}
Explanation
Lines 1–3: These lines make all the necessary imports.
Line 5: A class is declared namedÂ
TriggerAnimation. It inherits methods from the classÂMonoBehaviour, a base class in Unity for the scripts attached to the specificÂGameObjects.Line 7: It declares a private variable
animatorof typeAnimator. The variable stores a reference to the Animator component attached to the sameGameObjectas this script.Lines 10–13: The
GetComponentmethod is used to get a reference to theAnimatorcomponent attached to the sameGameObjectas this script. The reference is then stored in ouranimatorvariable. It allows us to use this reference to control theAnimatorcomponent from our script.Lines 16–32: An
ifcondition is used to check for the user input. TheInput.GetKeyDownmethod returnstruein the frame when the user presses thePkey, andfalseotherwise. If the method returnstrue, the line sets theScale 0parameter on theAnimatorcomponent totrueand it will play the scaling animation. If the user presses theSkey, it sets theScale 1parameter totruewhich plays the descaling animation. It will cause any transitions in theAnimatorController that are conditioned on the parameter being set and it plays the animation (scale the cube on pressingPand pressingScause the animation to stop).
Play the animation
Save all the changes.
Click on the play button to play the animation.
You should be able to see an animated cube which changes its size as specified in the keyframes.
Demonstration
Sample project
import React from 'react';
require('./style.css');
import ReactDOM from 'react-dom';
import App from './app.js';
ReactDOM.render(
<App />,
document.getElementById('root')
);
Press
Pto scale the cube andSto descale the cube.
Continue reading
Free Resources