This first recipe shows how to make a basic animation inside Unity. We will animate a light-intensity value to create an interesting flickering light effect, which is often found in horror or sci-fi games.
Getting ready
Before we start, you need to have a scene with geometry and at least one light. You can also download the example provided; open the project in Unity and go to the folder Chapter 02 Working with the animation view\Recipe 01 Using the animation view to create a flickering light. You will find a scene called Example.unity there, with a point light that has already been animated.
How to do it…
To use the Animation View and create a flickering light, follow these steps:
Open the scene and select a light you want to animate.
1.
With the light selected, go to Window | Animation. The Animation View will 2. open.
To create a new animation, click on the Create button, as shown in the following 3. screenshot:
A Create New Animation Clip will appear. You can choose a directory and the 4. animation name. Then, click the Save button.
A new, empty animation will be created, an Animator component will be added 5. to the selected game object (a light in this example), and an Animator Controller
will be created in the same directory as the animation clip.
To animate the light intensity, we need to add an Intensity property. Click on 6. the Add Property button in the Animation View, then choose Light | Intensity,
and then click on the + icon next to it, as shown in the following screenshot:
Two key frames will be added. You can click and drag on them to adjust their 7. position in time. You can also add new key frames by changing any property in
the light game object's Inspector (make sure to have the record button clicked in the upper-left corner of the Animation View).
Add several key frames with different values of light intensity.
8.
Close the Animation View when the animation is done. Any changes will be 9. saved automatically.
Play the game to see the effect.
10.
How it works…
The Animation View is divided into three main parts, as shown in the following screenshot:
Playback buttons (1): Here you can play back the animation on your scene (you do not have to enter Play Mode to see your animation). On the right to the play back buttons, you can find an Insert Key Frame button and an Insert Animation Event button. You can use the first one to insert key frames; the second one inserts Animation Events that allow to call script functions from an animation (Animation Events will be covered later in Chapter 6, Handling Combat). Just below the playback buttons, you can find a drop-down menu, with which you can switch between this object's animations and add new ones.
Animated properties (2): Here you can find all the properties of the game object used by this animation. You can also click on the Add Property button to add new properties to the animation.
Timeline (3): It represents the time in the animation. You can find all the key frames (and animation events) here. You can click on the timeline header (the one with time displayed in seconds) to go to the frame you clicked on. If you click on a key frame instead, the timeline will automatically jump to that exact key frame.
When you create an animation in the Animation View for the first time for a given game object, a new Animation Clip and an Animation Controller are created. The Animator Controller is automatically assigned to the game object you are animating and
the Animation Clip is added to the Animator Controller. Any new animation created for the same game object will be added to the same Animator Controller automatically.
There's more…
The timeline of the Animation View has two modes (you can switch between them by clicking on the corresponding buttons on the bottom of the properties section):
Dope Sheet: This mode displays key frames in the timeline.
Curves: With this mode, you can adjust the interpolation curves of the animation.
When you click on a property, its animation curves will be displayed. You can manipulate the handles to adjust the shape of the curves. You can also right-click on any handle to change its tangent type, as shown in the following screenshot:
All Animation Clips created in the Animation View are looped by default. To change this, find the Animation Clip asset, select it, and disable the Loop Time checkbox in its Inspector.
All the animations are created for selected game objects in the Hierarchy. To view an object animation, you need to select it and open the Animation View.
If you have multiple Animator components in your object's Hierarchy, a
new Animator Controller will be created for each Animator component. It may be useful to create different animations for different objects in the Hierarchy and play them simultaneously. For example, you can have a 2D sprite character with its head and body as separate game objects. You can use
two Animator components: one for the body, one for the head. If you choose so, you will be able to create facial expressions independently of the body animation.