Reader small image

You're reading from  Unity 5.x Animation Cookbook

Product typeBook
Published inMay 2016
Reading LevelExpert
PublisherPackt
ISBN-139781785883910
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Maciej Szczesnik
Maciej Szczesnik
author image
Maciej Szczesnik

Not interested. Too busy with current game project. Source: Linkedin.
Read more about Maciej Szczesnik

Right arrow

Chapter 7. Special Effects

This chapter explains how to achieve interesting effects with animations and covers the following recipes:

  • Using Animation Events to trigger sound and visual effects
  • Creating camera shakes with the Animation View and the Animator Controller
  • Using the Animation View to animate public script variables
  • Using additive Mecanim layers to add extra motion to a character
  • Using Blend Shapes to morph an object into another one
  • Using wind emitters to create motion for foliage and particle systems
  • Using sprite sheets to animate particles
  • Animating properties of a particle system with the Animation View
  • Using waveform of a sound clip to animate objects in the scene
  • Creating a day and night cycle with the Animation View

Introduction


We've learned a lot in the previous chapters. This chapter is all about encouraging you to experiment with Unity's animation system. During the next 10 recipes, we will create interesting effects and use animations in new, creative ways. This chapter also covers additional built—in Unity features that can be used to animate our scenes.

Using Animation Events to trigger sound and visual effects


This recipe shows a simple, generic way of playing different sound and visual effects with Animation Events.

Getting ready

To start, you need to have a character with one looped animation: Jump. We also need a sound effect and a particle system. We will need a transparent DustParticle.png texture for the particle system. It should resemble a small dust cloud. You can open the provided example Unity project and go to the Chapter 07 Special effects\Recipe 01 Using animation events to trigger sound and visual effects directory. In the Rigs directory, you will find all the needed animations, and in the Resources folder, you will find all other necessary assets. When you play the game, you will see a character playing the Jump animation. It will also play a sound effect and a particle effect when landing.

How to do it...

To play sound and visual effects with Animation Events, follow these steps:

  1. Import the character with the Jump animation...

Creating camera shakes with the Animation View and the Animator Controller


In this recipe, we will use a simple, but very effective method for creating camera shakes. Those effects are often used to emphasize impacts or explosions in our games.

 

Getting ready

You don't need anything special for this recipe. We will create everything from scratch in Unity. You can also download the provided example Unity project and go to the Chapter 07 Special effects\Recipe 02 Creating camera shakes with the animation window and the animator controller directory. When you open the  Example.unity scene and play the game, you can press the space bar to see a simple camera shake effect.

How to do it...

To create a camera shake effect, follow these steps:

  1. Create an empty game object in the Scene View and name it CameraRig.
  2. Parent the Main Camera to the CameraRig.
  3. Select the Main Camera and add an Animator component to it.
  4. Open the Animation View.
  5. Create a new Animation Clip and call it CamNormal. The camera should have...

Using the Animation View to animate public script variables


In Unity, we can animate public script variables. Most standard types are supported. We can use this to achieve interesting effects that are not possible to achieve directly. For instance, we can animate fog color and density, which is not directly accessible through the Animation View.

Getting ready

In this recipe, everything will be created from scratch, so you don't need to prepare any special assets. You can also use the example project and go to the Chapter 07 Special effects\Recipe 03 Using the animation window to animate public script variables directory. You can find the Example.unity scene there. If you open it and press the space bar, you can observe the fog changing color and density.

 

It is achieved by animating the public variables of a script.

Animated fog

How to do it...

To animate public script variables, follow these steps:

  1. Create a new script and call it FogAnimator.cs.
  2. Create two public variables in this script: public...

Using additive Mecanim layers to add extra motion to a character


In previous recipes, we were using Mecanim layers in override mode. We can set a layer to be additive. This can add additional movement to our base layer animations.

Getting ready

We will need a character with three animations: IdleTiredReference, and Tired. The Idle animation is a normal, stationary idle. The TiredReference animation has no motion and is used as a reference pose for calculating additive motion from the Tired animation. The TiredReference animation can be the first frame of the Tired animation. In the Tired animation, our character is breathing heavily.

You can also go to the Chapter 07 Special effects\Recipe 04 Using additive mecanim layers to add extra motion to a character directory. Open the Example.unity scene there. You will find the same Humanoid character there. If you play the game and press the space bar, our character will start breathing heavily while still playing the Idle animation. You can find...

Using Blend Shapes to morph an object into another one


Previously, we used Blend Shapes to create face expressions. Blend Shapes are also an excellent tool for special effects. In this recipe, we will morph one object into another.

Getting ready

To follow this recipe, we need to prepare an object with Blend Shapes. We've created a really simple example in Blender—a subdivided cube with one shape key that looks like a sphere. See the following screenshot for reference:

A cube with a Blend Shape that turns it into a sphere

You can also go to the Chapter 07 Special effects\Recipe 05 Using blendshapes to morph an object into another one directory. Open the Example.unity scene there. You will see a number of cubes there. If you hit the space bar in play mode, the cubes will morph into spheres. You can find the Cuboid.fbx asset with the required Blend Shapes in the Model directory.

How to do it...

To use Blend Shapes to morph objects, follow these steps:

  1. Import the model with at least one Blend Shape...

Using wind emitters to create motion for foliage and particle systems


Unity supports wind emitters with which we can create realistic effects for foliage and particles. Using them is really simple.

Getting ready

To follow this recipe, you need to have a particle system and a tree created with Unity's built—in Tree Creator. You can open the provided example project and go to the Chapter 07 Special effects\Recipe 06 Using wind emitters to create motion for foliage and particle systems directory. In the Example.unity scene there, you can find a tree and a simple particle system (falling leaves). Play the game to see the tree and particles moving with the wind.

Tree with a falling leaves particle system. Both animated with a WindZone

How to do it...

To use Unity's wind, follow these steps:

  1. Place the tree and the particle system in the scene.
  2. Create a new WindZone. To do so, go to the GameObject3D ObjectWind Zone option.
  3. The tree will move when you play the game, but the particle system needs to...

Using sprite sheets to animate particles


We can add sprite sheetanimation to Unity's particles to make them more interesting. This concept is especially useful for various explosions and magical effects.

Getting ready

To follow this recipe, we need an animation exported as a sprite sheet. If you want to know how to create a sprite sheet animation, you can read the Exporting a 2D sprite animation from a 3D package recipe in Chapter 32D and User Interface Animation, where you can find an example sprite animation.

Example sprite sheet animation

You can also open the provided example project and go to the Chapter 07 Special effects\Recipe 07 Using sprite sheets to animate particles directory. If you open the Example.unity scene there and play the game, you will see a particle system with explosions using sprite sheet animation. You can find the necessary sprite sheet in the Rigs directory.

 

How to do it...

To use sprite sheets for particles animation, follow these steps:

  1. Import the sprite sheet texture...

Animating properties of a particle system with the Animation View


This recipe shows how to use the Animation View to animate a particle system's properties. Not all of them can be directly accessed with the Animation View, but we will use the same trick as with the fog in the Using the Animation View to animate public script variables recipe.

 

Getting ready

This recipe is created from scratch in Unity; you don't need any special assets. You should have a particle system ready before we start. You can go to the Chapter 07 Special effects\Recipe 08 Animating properties of a particle system with the animation window directory. You will find an Example.unity scene there. Open it and play the game to a particle system with animated properties.

How to do it...

To animate particle system properties, follow these steps:

  1. Create an empty game object and name it AnimatedParticleSystem.
  2. Add a Particle System component to it.
  3. Create the particle system to your liking. We have a very simple particle system with...

Using waveform of a sound clip to animate objects in the scene


In this recipe, we will use a waveform of a sound effect to create motion based on the average volume of the fragment of the waveform.

Getting ready

To follow this recipe, we need a sound effect. You can use the provided example Unity project and go to the Chapter 07 Special effects\Recipe 09 Using waveform of a sound clip to animate objects in the scene directory. If you open the Example.unity scene there and play the game, you will be able to see a few cubes react to the playing audio clip.

How to do it...

To use sound waveforms for animation, follow these steps:

  1. Import the sound into Unity.
  2. Create an empty game object and name it AudioSource.
  3. Add an Audio Source component to it, and make the sound Loop and Play On Awake. Drag and drop your sound into the AudioClip field.
  4. Create a Cube game object (go to Game Object3D ObjectCube).
  5. Create a new script and call it ScaleWithWaveForm.cs. In this script, we have one public AudioSource...

Creating a day and night cycle with the Animation View


In this recipe, we will create a simple day and night cycle animation using the Animation View.

Getting ready

We are going to create this recipe from scratch in Unity, so we don't need any additional assets. You can also use the provided example Unity project and go to the Chapter 07 Special effects\Recipe 10 Creating a day and night cycle with the animation window directory. You will find an Example.unity scene there.

 

Open it and play the game to see the effect.

Simple day and night cycle

How to do it...

To create a day and night cycle with the Animation View, follow these steps:

  1. Create an empty game object and call it DayAndNight.
  2. Create another empty game object, call it SunAndMoonPivot, and parent it to the DayAndNight game object.
  3. Create two more game objects, SunPivot and MoonPivot, and parent them to SunAndMoonPivot.
  4. Then create the Sun and Moon objects and parent the first one to SunPivot and the second one to MoonPivot.
  5. Lastly, create...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity 5.x Animation Cookbook
Published in: May 2016Publisher: PacktISBN-13: 9781785883910
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Maciej Szczesnik

Not interested. Too busy with current game project. Source: Linkedin.
Read more about Maciej Szczesnik