Hide chapters

Unity Apprentice

First Edition · Unity 2020.3.x LTS Release · C# · Unity

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

12. Basic Animation Principles
Written by Ben MacKinnon

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Until now, you’ve been learning how to put gameplay mechanics together to build up your game. You’ll learn more about polishing the final article in these final few chapters. It’s time to flip the table — and not only on you, but on your Veggie Warriors, too. :]

In this chapter, you’ll learn the basics of animation in Unity. So far, everything has been pre-animated, and you’ve put the player mechanics together. Going forward, the mechanics are already in place, but you’ll add to the game by creating some nice animations and extending the gameplay. For this, you’ll look at a new game environment - the kitchen! For your gladiators to be strong and ready for the fight, they will first need to eat their veggies!

Introduction to Unity animation types

Just like there are many ways to peel a vegetable — whether it’s a trusty peeler, a new-fangled machine or the edge of a blunt knife — there are many ways you can animate objects in Unity.

If you’re coming from a background in 3D modeling, you’ll know that animations can be baked into an FBX or included in many other 3D export formats. Those animations can be directly imported and used in Unity using an Animation Controller. You’ll get to that later. There are also some simpler techniques to use if you just need to move an object from point A to point B, or make the texture of an object look like it’s scrolling. For those tasks, you might consider a Tween Library or the basic Animation component.

Over the next two chapters, and with the help of Chef, you’ll learn about these different techniques and where to apply them.

Introducing the kitchen

You might be wondering what happened to all those Veggie Warriors that fell in battle against the tank. Well, you’re about to find out. Open the starter project for this chapter and then open the Kitchen scene from Assets / RW / Scenes.

Deciding on how to animate your GameObjects

There are many ways to animate GameObjects inside of Unity. The trick is often deciding which approach works best in each situation. You have to consider some of the following facts:

Building a tween library

Tweening is a very common method of creating animations in Unity. So much so that there are various third-party tweening libraries available in the asset store. Some of the most popular include DOTween and LeanTween. Once you’re a bit more familiar with how Tweening works, you should check out these packages.

Filling the trough

In the Hierarchy, find one of the Troughs under the Interactables parent GameObject. Select it and take a look at the PickupArea component. You’ll see three key things have been set up:

private void Spawn()
    // 1
    if (availableIngredients.Count < spawnPositions.Count)
        // space for more ingredients
        // 2
        IngredientObject newIngredient
            = IngredientPool.Instance.Fetch(ingredientType);
        if (newIngredient != null)
            // 3
            // 4
            newIngredient.transform.parent = transform;
            // 5
    // 6
    timeSinceSpawn = 0;

Lerp logic

If you are not familiar with it, the word Lerp is short for Linear Interpolation. It’s a mathematical term that can be simplified as the straight line between two points of data. Using this line, you can work out any position between these two points.

teyf(a, h, c) = i + (r - o) * t Dfitp Noymayjehe Gohdexwa zfeg A ca N

Wuqbuqmegu Fitei 0 7 6 0.9 0 2

public static void Lerp(this MonoBehaviour m,
                        Transform from,
                        Transform to,
                        float time)
    m.transform.position = from.position;
    m.transform.rotation = from.rotation;

    m.StartCoroutine(Lerp(m.transform, to, time));

Creating the Lerp function

Note: You’re going to use Coroutines and IEnumerators again here. If you want a quick refresher on them, please review the previous chapter.

private static IEnumerator Lerp(Transform transform,
                                Transform target,
                                float time)
    yield return null;
float elapsedTime = 0;
Vector3 startPos = transform.position;
while (elapsedTime < time)
    yield return null;
elapsedTime += Time.deltaTime;
transform.position = Vector3.Lerp(startPos, target.position, time);
All this lerping around is getting tedious.
Ash wnij pidmang eloiyv ig vinpoqw lodauex.

naby(i, k, v) = i + (n - o) * m Ygeyk Dohmacbibi Wowfegvo xduj I wo T

transform.position = Vector3.Lerp(startPos,
                                  elapsedTime / time);
transform.position = target.position;

Dropping the veggies into the troughs

Save your changes and return to the Unity editor. Click Play and watch as the Veggies now fall into the troughs. Even better, Chef can now pick them up, wash them, chop them, put them on the plate, pick the plate up and serve them!

All that lerping around really paid off!
Oxd mmeg budqekv efeibz naamng foiv ozb!

Quaternion startRot = transform.rotation;
transform.rotation = Quaternion.Lerp(startRot,
                                     elapsedTime / time);
transform.rotation = target.rotation;

Setting up a basic animation

Sometimes you might want a very simple looping animation just to bring life to a scene. Take a look at the serving table in the scene. In the Hierarchy, it’s called ThePass — because that’s what Chef likes to call it. Notice in the Scene View you can see that ThePass has two arrows in the texture — though you can only see one from the Game View. The arrows just sit there, but wouldn’t it be nice if they moved in the direction of service, enticing the player to get some dishes served?

Introduction to the Animator component

Expand ThePass to reveal its children, and navigate down to the Arrow child GameObject.

Moving the serving table arrows

OK, time to go back to that Animation window. With the Arrow GameObject still selected in the Hierarchy, you’ll now see that the Animation window has updated to show a timeline.

Modifying animation curves

It’s time to take a look at the other view inside the Animation window. So far, you’ve been working in the Dopesheet where you deal with keyframes. Now you need to switch over to the Curves editor.

Key points

  • Unity has many different ways to build animations.
  • Always think about what approach is best for your situation.
  • Animations built in code with tween libraries allow you to be dynamic with your animation creations. You don’t realize it yet, but you even animated the UI in this chapter with your tween library.
  • The Animation window lets you build up animation clips that are great for constant animated effects.
  • The Curve editor lets you create all kinds of varied animations.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now