At this point, you’ve learned how to move GameObjects in Unity in various ways, including:
- Writing scripts to manually change the position. You learned this in Chapter 3, “Components,” when you wrote scripts to move the player, camera and bullets.
- Using the Character Controller. You learned this in Chapter 4, “Physics,” when you replaced the player movement script with the built-in Character Controller, which has powerful functionality and is often easier than writing movement scripts on your own.
- Letting the Unity physics engine move objects for you. You learned this in Chapter 4, “Physics,” when configured the space marine’s bobblehead by attaching it to the body with a joint and periodically applying a force to make it bobble.
- Employing the navigation system to move objects on your behalf. You learned this in Chapter 5, “Pathfinding,” when you configured the aliens as a NavMesh agent and gave it a target so that they could pathfind their way to the space marine.
There’s another important way to move GameObjects in Unity that you haven’t explored yet: The animation system, otherwise known as Mecanim. Mecanim is a powerful system that allows you to animate models as well as every single exposed property in Unity.
That’s right, if you can set a value in the Inspector, and you can animate that value over time! Powerful stuff.
Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.
Getting started
The first animation that you will create is a simple animation to make the arena walls go up and down. This is because, currently, there’s an issue. As the space marine moves towards the camera, the wall blocks your view of him. This is bad mojo; not only can you not see what you’re shooting, but the spawning aliens can attack instantly.

Causing people to lose because their view was blocked is no way to win gamers’ hearts and minds!
One technique to solve the problem is to make the wall transparent, but that doesn’t solve the problem of the partially obscured play area. For slow-paced games, it wouldn’t be a serious problem, but fast reaction times are required to win Bobblehead Wars.
To solve the problem, you’ll lower the walls of the arena when the player gets close and raise them when the player walks away.
The animation window
Open your project from Chapter 5, “Pathfinding.” If you’re reading these chapters out of order, feel free to open the starter project that’s located in the resources for this chapter. You can download it here:
https://drive.google.com/file/d/1BxRPAECCPPGweqGycbBtgdPkMh8Qo-pe/view?usp=drive_link
You’re going to animate various child GameObjects of the BobbleArena. You could animate them individually, but this technique will have you grouping child GameObjects together, which is far more efficient.
In the Hierarchy, select the BobbleArena GameObject. Click Window ▸ Animation to open the Animation window.
The window has several sections, and you’ll spend quite a bit of time here in this chapter.

On the left, you have a Property List where you select the properties you intend to animate. On the right, you have the Dope Sheet where you create keyframes — more on this in a moment.
Above the dope sheet is the Timeline where you scrub back and forth on the animation to preview how it looks. Finally, above the property list, you’ll see Frame Navigation where you control playback and add animation events.
Animation events allow you to run code at certain frames in an animation.
Introducing keyframe animations
Unity animates objects through the use of keyframe animation.
In keyframe animation, you set up a series of pairs (time, value) to indicate how you want an object to animate over time. You don’t need to specify every single second; the beauty is that the game engine will automatically calculate the values in between your key frames.
For example, say you have a cube at (0, 0, 0)
that you want to move to (0, 10, 0)
two seconds later. Then you would set up two keyframes:
- Time 0 seconds: Position
(0, 0, 0)
- Time 2 seconds: Position
(0, 10, 0)
Where would the cube be at time one second in your animation? Unity would automatically calculate this for you through interpolation — for example, (0, 5, 0)
.
Keyframe animation makes creating animations easy. You just have to specify the important points — again, the keyframes and Unity takes care of the rest!
Creating an animation
Select the BobbleArena in the Hierarchy. In the dope sheet, click the Create button.

Give it the name Walls.anim and save it in the Animations folder. While you were busy saving, Unity was busy behind the scenes:
- Created a new animation clip
- Created a new animation controller
In the Hierarchy, select BobbleArena and see it for yourself in the Inspector.

Unity only does these things when you create a new animation on a GameObject without an animator controller. It attaches an Animator Controller on the GameObject, but it also creates an Animator Controller file in the same folder as your animation clips. This file keeps track of all your states, about which you’ll learn in just a bit.
Now to the part in which you actually make the animation!
Click the Add Property button in the Animation window

You’re in the place where you select the GameObject or property that you wish to animate. You’ll see each component listed, as well as the child GameObjects.
Expand the Floor disclosure triangle and then expand the Outer_Wall_003 GameObject. Next, expand the Transform component and click the plus sign next to the Position property.

Do the same thing for Outer_Wall_004 and Outer_Wall_006.

Recording an animation
At this point, you’re technically ready to start recording the animation. Do you feel ready? Huh? Do you?
Good! Click the little red record button in the Animation window with authority.

You’ll notice that the entire animation interface is red, letting you know that you are currently recording the animation.
There are two different modes when working with animation: Preview mode and Record mode.
When pressing the Preview button in the timeline, you’ll notice that the timeline changes to a light blue color. This means that Unity will only record changes made to the current property being animated. Changes to other GameObjects won’t be recorded.
If you press the record button, Unity will record any changes that you make in your scene. If a property is not present on the timeline, Unity will automatically add it for you. Changes to other GameObjects will be recorded.
If you are animating specific properties, then work in Preview mode. If you need to animate a large selection of GameObjects, make sure to animated in Record mode. For now, you’ll work in Record mode.
If you select one of the GameObjects that you’re animating, you’ll also notice that its position properties turn red.

These are all visual reminders that you’re currently in Record mode. Had you clicked the Preview Mode button in the animation timeline, the buttons would have been light blue.
Go to the Dope sheet and click the timeline above the keyframes at the 1:00 mark.

Since you are in Animation mode, you can think of this as “time traveling” to what the animation will look like at the 1:00 mark. You can see what it will look like in the Unity editor, and modify the values.
Note that if you don’t move the scrubber exactly over the keyframes at the 1:00 mark, Unity will create new keyframes at the scrubber’s current position.
Make sure that just Outer_Wall_003 is selected and, in the Inspector, set the y position to -0.48
. In the Scene view, you’ll see the wall drop down.

Do the same for Outer_Wall_004 and Outer_Wall_006. The walls should look as follows:

Now, click the animation Play button (in the Animation window, not the normal Play button). Just like that, you have moving walls!

Hit the animation Play button again to stop the looping animation. Note you can also move the scrubber on the timeline to see the interpolated values at any point.
When you finish, click the Record button to stop Record mode.
Where to go from here
Using the animation editor is a quick and easy way to animate your GameObjects. You’ll also be doing a lot of animation in code. There are several animation libraries for you to use but that’s beyond the scope of this tutorial.
Next, you’ll actually run your animation by way of something called the animator. You’ll do this by creating animation states and you’ll do this in the next tutorial.
Discover more from Jezner Blog
Subscribe to get the latest posts sent to your email.