Create and Play animation in Unity
This article provides step by step guide to create and play animation in unity game engine.
Download Unity Package for Sample Project (Unity 5.6.1): AnimationDemo
Create Animation in Unity
- Create a new project in unity and a cube game object in your scene. Your scene will look this below given image. I have scaled the cube object and changed its color but that is not mandatory.
- Navigate to Windows -> Animation in unity editor. It will open the animation panel. You can adjust the position of this panel upon your requirement.
- Select cube game object in scene. A create button will appear on animation panel. Click on Create button to generate animation for selected game object.
- On clicking the Create button, animation panel will show to add property for the animation.
- Click on Add Property button. It will show you the list of properties that you can add in the animation.
- For this tutorial, I am adding Position and Rotation properties. By default, you will get 60 consecutive samples, on which you can change values of these properties.
Details of marked numbers in above image:- Recording: Hit this button if you want to record transformation or any other changes on the selected game object. See implementation in next step.
- Play: Play the recorded animation
- Samples: You can change the sample size here.
- Red Marker: change this marker position to add new values to the properties
- Sample time line: Place above marker to this sample values.
You can drag and place the red line in sample time line to put your customized value for the animation. I am adding one stop point at 30th sample and changing the position and rotation values. Then again, I will do the same for 60th sample.
- Alternatively, you can click on record button and transform you game object. This transformation will be recorded in an animation file that you can play later. If you will click on the recording button then you “Play, Pause and Stop” button will display in red color.
- Click on play button in animation panel to see how animation play. Modify the values in animation panel if required.
Play Animation in Unity
- The unity editor would have generated two files by now – animation and the animator controller. The animator component will be attached to the Cube game object.
- By default, the animation will play in loop. Select CubeAnimation.anim and uncheck Loop Time property in inspector. Now the animation will play only once.
- Open Cube.Controller. You will see CubeAnimation state over there will be the default state of the controller. It mans at runtime this state will be played automatically.
- Now we can test our application. Hit the play button.
That’s it for this tutorial. Please post your comments for queries and feedback. Thanks for reading.
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.
Latest posts by Gyanendu Shekhar (see all)
- Using Transparent Material in Unity 3D - February 8, 2021
- Getting started with UI Toolkit : Unity 3D Tutorial - December 30, 2020
- Using Events in Unity 3D - May 2, 2020
Very well written
Thanks Praveen 🙂