After my last post discussing the features within Unity’s Mecanim animations tools, I thought it would be a good opportunity to show you how to animate a cube using Mecanim’s basic keyframe animation system.
- To start, create a fresh scene
- Save the scene immediately so you don’t lose any progress. Start by adding a basic cube in the scene at (0, 0, 0).
- On the cube, add an Animator component by clicking Add Component -> Animator
- In the Project tab, add an Animation Controller
- Link the Animation to the Animation Controller in the inspector
- Initially, we need to see the animations that we want to create. So, we need to reveal the Animation window, in the Window menu drop-down.
- Here, you have the Animation Window. Click “create” to start your
- Save your animation as whatever you think it appropriate
- The Animation Window will come to life with a timeline, and the “Add Property” button
- Mecanim works on animating “Properties”, which are components which you can already edit within the inspector. Anything you can edit in the inspector can be used as a property in the timeline. To start, add the Transform -> rotation property to the
- You’ll see that the pause/play/next button as well as the rotation properties will become red – an indicator that you are current animating that component.
- We want to animate a rotating cube, so we will place a keyframe at the half-way mark and set the Y property as 180 – a complete reversal
- Finally, set the last keyframe as 360, but do so within the Animation window, because the inspector will round it down to 0, which you can see for yourself if you want!
Press play, and you will see your cube spinning like a top! These basic principles can be applied throughout Unity, such as to animate audio effects, and movement systems (like spinning wheels). Feel free to experiment with what Mecanim will do!