Unity Animation Clips

Unity Prefabs

Prefabs are collections of models or elements that can be packaged together for easy placement into a scene. These prefabs are restricted when placed into a scene while they are packages. To edit or control a certain element of the prefab, you must unpack completely. Select the object in the hierarchy RIGHT CLICK > PREFAB > UNPACK COMPLETELY.

Object Pivots

Models come with origins, or pivots. anything modelled in Blender, for example, with have a pivot point, from which it will determine the objects translation values. In Unity there are 2 options when selecting an object - CENTER (the general center if the object based on its occupied space) and PIVOT (The origin point). This can be switched at the top left of the viewport.

When animating, pivot is essential as this is the point around which the item will rotate. In this example the origin is not where we need it, meaning it will not animate correctly. Unfortunately, origin points are set when exporting from a modelling package, so keep an eye on this if you are modelling. Fortunately there is a technique to adjusting and controlling pivots inside of Unity.

Make an EMPTY object and place it where you need a pivot. in this case in the middle of the turbine.

Make the windmill turbine model a child of the empty by dragging it ONTO the object in the hierarchy. You will then see the model is a child of the object, sitting inside below it. This is a parent relationship, meaning the child will inherit any adjustments we make to the parent.

Creating Animation Clips

Animations can be keyframes and exported from modelling software such as blender in clips, and imported and applied in game engine. Unity also give you the ability to create animation clips in the engine. Access the animation tab via WINDOW > ANIMATION > ANIMATION

Select a game object in the hierarchy, in this case the EMPTY game object (now the parent of the windmill turbine) and click create. Give the animation a name

Press record and make a slight movement to drop a keyframe. In this case rotation on the X axis. You will see it will drop a keyframe . Set the value back to zero

Move to another point on the animation timeline, 1.00 second for example, and adjust the rotation to 360. This will drop a second keyframe. Unity will interpolate between these 2 recorded values, making the in-between frames for the object to reach the second value.

Animation Graph Editor

Keyframe animations automatically apply Easy Ease (one of the 12 principles of animation) making the object start and end slowly, much like a car accelerating and decelerating. In this case this is not what we want. Speed and timing can be adjusted via the CURVES found at the bottom of the animation panel.

This view provides a graph data displaying all animated channels of the selected object. The vertical bar represent values, and the horizontal representing time. In this example we can see a curve as the animation starts, and again as the animation ends. This shows us there is acceleration and decelaration (speed over time).

The input and output of a keyframe can be adjust, much like anchor points of a vector - these are called tangents. Select a keyframe and right click and go to tangants. This case we want to adjust both input and output, so select BOTH > LINEAR. repeat this for all keyframes for this rotation animation.

A constant speed over time will no longer adjust the timing, so the rotation animation will loop continuously.

Multiple Animations

Using what we have just covered, go through the process to make a new pivot (hinge) for the door on the front of the windmill. After which, create 4 door animations - CLOSED IDLE, OPENING, OPEN IDLE, CLOSING. To make new animation clips for animation, click the dropdown and create new

All animations created will sit in your asset folder. Selecting an animation clips will display information such as speed and looping etc. For the OPEN and CLOSE animation, turn off lopping. It's a good idea to create an animation folder to organise animation clips

Animator Transitions

Any animated object will automatically add an animator controller, allowing you to control animations playing. Access the ANIMATOR window by double clicking a controller or by going to WINDOW > ANIMATION > ANIMATOR. here you can see, and add, any animation clips for the selected object.

((add image of animator window))

By default you will see one animation being activated upon playing the game, this is the default state, which will loop until other animations are linked. Select the CLOSED IDLE animation, right click and make default state, so the game starts with this animation playing.

((add image of default state))

Right click on CLOSED IDLE animation and select transition, drag the arrow onto OPENING animation clip. This creates a transition path, after the CLOSED IDLE is finished playing, it will move onto the OPENING animation.

((add image of transitions))

Make transitions to each respective animation and loop back to the DOOR IDLE. This will create an animation loop that the animator will cycle through.

Animation Transitions

Animation transitions can be controlled, and activation upon certain conditions being met. Select PARAMETERS and add 2 Triggers, one named DoorOpen and the other DoorClosed.

Selecting a transition allows you to now define your parameters as conditions to be met for the transition to execute the next animation. Select the transition between CLOSED IDLE and OPENING. Under conditions, set this to DoorOpen. Repeat this process for the transition between OPEN IDLE and CLOSING.

The Animator is now all setup to play and cycle animations upon conditions being met. Now we need to add a trigger zone to our door, that can be coded to activate conditions when the player approaches and moves away. Do this by adding a new COLLIDER component and setting it as a trigger to the game object.

Adjust the trigger zone so it is large enough for the player to enter and exit. A sphere collider would be a good option for this situation.

Now that is setup, we need to write some simple code. Create a new C# script on the EMPTY game object. Access VS to write the code

Scripting Animation Transitions

We need to define parameters of our code, in this case the animator. We then need to ensure we are acquiring the correct component for this define parameter.

We will then create methods to be executed upon entering and exiting the trigger zone. For this you will make 2 new methods, OnTriggerEnter and OnTriggerExit.

If statements determine that certain details need to be true to execute the code, in this case we will use a tag system, so that the animation only executes when the PLAYER enters or exits the trigger zone. This refers to the object tag, which can be selected or customised in the inspector - as you can see on the existing player object.

After the defining the tag, complete the code by defining the conditions to be met, as setup in the Animator.

Playthrough and approach the door to check the animations are triggered. Move out of the zone to check the exit animation is also working. Refer to my recorded tutorials below for a demonstration.

Overview of animation clips
Step-by-step of turbine setup and clips
Setup of triggers and script

Last updated