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 0 of type Bool to scale the cube.

  • Create another parameter named Scale 1 of type Bool to descale the cube.

Note:

  • Setting the parameter type to Trigger will play the animation automatically when the scene is rendered.

  • To control the animation, you can use the Bool parameter type.

  • Click on the arrow pointing to the animation in the animator window.

  • Add the Scale 0 parameter in the conditions tab (trigger is automatically reset after it has been used but the bool parameter has to be set to false after the transition is finished).

  • Adjust the Scale 1 parameter 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 0 parameter 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 initialization
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void 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 animator of type Animator. The variable stores a reference to the Animator component attached to the same GameObject as this script.

  • Lines 10–13: The GetComponent method is used to get a reference to the Animator component attached to the same GameObject as this script. The reference is then stored in our animator variable. It allows us to use this reference to control the Animator component from our script.

  • Lines 16–32: An if condition is used to check for the user input. The Input.GetKeyDown method returns true in the frame when the user presses the P key, and false otherwise. If the method returns true, the line sets the Scale 0 parameter on the Animator component to true and it will play the scaling animation. If the user presses the S key, it sets the Scale 1 parameter to true which plays the descaling animation. It will cause any transitions in the Animator Controller that are conditioned on the parameter being set and it plays the animation (scale the cube on pressing P and pressing S cause 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

Create a GameObject
1 of 18

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')
);
Copyright ©2024 Educative, Inc. All rights reserved