Create and play animation at runtime: Unity Tutorial

This article explains how to create and play animation at runtime. First we will create a animation clip at runtime and then we will play it using:

  • Animation Component
  • Animator Component

I have created a simple project for demo. I have added a simple sphere gameobject in the scene, on which animation will be played.

Create and play animation at runtime

Create and play animation at runtime: Animation Component

  • Add animation component to the Sphere GameObject.
    Create and play animation at runtime
  • Add below given script to the Sphere Object. The script creates and plays the animation clip using animation component. Set the animation clip to be legacy before using it with animation component.
  • Run the application.
    Create and play animation at runtime

Animation Clip

The above example creates animation clip to change position of the GameObject. But, we can also create animation clip to change rotation and scale of the GameObject.

Animation clip to change Scale

Animation Clip to change Rotation

Create and play animation at runtime: Animator Component

We are going to use AnimatorOverrideController to replace default animation clip of an animator state.

Step 1: Play a default animation using Animator component

  • Create a AnimatorController in the Assets panel (Create -> AnimationController). Here, the name of the controller is SphereController.
  • Create a empty state in the controller. Set this as the default state. Add any animation clip to this state. Here, name of the state is “test” and i have added “sphereanim” animation file to it. I have already created this animation file beforehand. You can use any animation file of your choice.┬áLater we will override this animation clip using AnimatorOverrideController.
    Create and play animation at runtime
  • Add Animator component to the Sphere GameObject. Attach the SphereController to this animator.
    Create and play animation at runtime
  • Run the application. It will play the animation file added to the default state of the controller.
    Create and play animation at runtime

Step 2: Replace the default animation using Animator Override Controller

  • Attach below given script to the sphere controller. The script overrides the default animation clip (sphereanim) of SphereController. Here, don’t mark animation clip to legacy.
  • Run the application. It will override the default animation file and will play the newly created animation file at the runtime.
    Create and play animation at runtime

Hope you get an idea to create and play animation at runtime in unity3d. Please post your comments for queries and feedback. Thanks for reading.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Leave a Reply

Your email address will not be published. Required fields are marked *