Reader small image

You're reading from  Unity 2020 Virtual Reality Projects - Third Edition

Product typeBook
Published inJul 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781839217333
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Jonathan Linowes
Jonathan Linowes
author image
Jonathan Linowes

Jonathan Linowes is a VR/AR enthusiast, Unity, and full stack developer, entrepreneur, certified Unity instructor, and the owner of Parkerhill XR Studio, an immersive media, applications, and game developer. Jonathan has a bachelor of fine arts degree from Syracuse University, a master of science degree from the MIT Media Lab, and has held technical leadership positions at Autodesk and other companies. He has authored multiple books on VR and AR published by Packt Publishing.
Read more about Jonathan Linowes

Right arrow
Animation and VR Storytelling

The stories we tell, and how we tell them, say a lot about who we are and what we will become. Storytelling between humans is as primal as any human activity and the basis of interpersonal communications, mythology, religion, historical record, education, entertainment, and all of the arts. VR is emerging as one of the newest, and potentially most profound, storytelling media formats.

In the previous chapter, we looked at 360-degree media, which itself is becoming its own medium for VR storytelling, especially for non-fictional documentaries, which are capable of transmitting human experience and creating immersive empathy for humanitarian crises. Many of the tools and lessons we will cover in this chapter can also be used with 360-degree media, but we're going to focus on 3D computer graphics and animation here.

For this project, we are going to create a little VR experience...

Technical requirements

To implement the projects and exercises in this chapter, you will need the following:

  • A PC or Mac with Unity 2019.4 LTS or later, the XR plugin for your device, and the XR Interaction Toolkit installed
  • A VR headset supported by the Unity XR platform

You can access or clone the GitHub repository for this book (https://github.com/PacktPublishing/Unity-2020-Virtual-Reality-Projects-3rd-Edition-) to optionally use the following assets and completed projects for this chapter:

  • The asset files for you to use in this chapter are located in the UVRP3Files/Chapter-12-Files.zip folder.
  • All the completed projects for this book can be found in a single Unity project atUVRP3Projects.
  • The completed assets and scenes for this chapter can be found in the UVRP3Projects/Assets/_UVRP3Assets/Chapter12/folder.

Additional assets are required to run this project, as described in...

Composing our story

OUR STORY: Music begins. You start in a dark scene and notice a small tree sapling in the ground in front of you. It starts to grow into a full-sized tree. As dawn breaks, a bird's nest appears, and we notice it has an egg in it. The egg begins to shake and then hatches. A baby bird emerges, hops around, grows, and tests its wings. Finally, in daylight, it flies away to freedom.

Our story is about birth, growth, spreading your wings (literally and figuratively), and moving on. We will start with a music soundtrack and animate our graphics based on its parts.

We will be using free, off-the-shelf assets. Of course, you can use your own music and graphics, but we'll assume you're following along using the ones we have selected, which are all available online for free (links will be provided where necessary). As an instructive project, it's minimalist and not embellished with effects that you might expect...

Timelines and Audio tracks

Earlier, we planned out the movie using a graph paper timeline. Unity provides a Timeline tool for implementing this almost directly. Timelines consist of one or more tracks that play over time. Timelines can control many different objects and different types of tracks. As we'll see and explain as we build this project, Timelines can have Audio Tracks, Activation Tracks, Animation Tracks, Signal Tracks, and more. Timelines are a type of Unity Playable.Playables are runtime objects that "play" over time, updating each frame based on their prescribed behavior. Animations are playables too. For more details, see https://docs.unity3d.com/ScriptReference/Playables.Playable.html.

Confirm that the Timeline package has been installed with your project (ordinarily, it is installed by default) and add or upgrade it if necessary using the following steps:

  1. Open Package Manager using Window | Package Manager.
  2. Select...

Using a Timeline to activate objects

According to our plan, when the timeline starts, the bird's nest will be hidden (the NestAndEggobject). At the 35-second mark, it becomes enabled. Also, when the nest is first enabled, it should containWholeEgg. Then, at the 80-second mark, it is hidden theHatchedEggis enabled instead. The NestAndEgg game object hierarchy, as shown here, contains the Nest itself, a WholeEgg object, and a HatchedEgg object (which contains the two eggshell halves):

Now, let's add the activation sequence to the Timeline. You can view the results of these and the following steps in the screenshot further down:

  1. With Blackbird Director selected in Hierarchy, drag the NestAndEgg object from Hierarchy into the Timeline Editor window.
  2. A menu will pop up, asking you what type of track to add; choose Activation Track.
  3. Ensure the Timeline units are Seconds, not Frames. Use the gear icon...

Recording an Animation Track

As you would expect, in addition to audio and activation tracks, Timelines can include animation tracks. Unity's animation features have evolved over the years and Timeline greatly simplifies basic animation capabilities within Unity. You can create and edit animations directly within Timeline without having to create separate Animation Clips and Animator Controllers. We will look at these in more detail later in this chapter. For now, we will start simple by animating just a few Transform parameters on the tree and the bird, thus adding a growing tree and a growing bird to the scene.

A growing tree

We want to animate the tree so that it grows from being small (scale 0.1) to full size during the first 30 seconds of the Timeline. We'll do this by adding an Animation Track for the Tree object, and then recording the parameter values at each keyframe time:

  1. Ensure Blackbird Director is selected in Hierarchy...

Using the Animation editor

Next, we'll create another animation track in order to animate the nest so that it starts positioned in the grown tree and then drifts slowly to the ground, wafting like a falling leaf. We want it to exhibit a gentle rocking motion. This is a little more complicated than the simple two-keyframe animation we just did, so we'll do our work in a separate Animation Window instead of the narrow track band on Timeline Editor. It will animate from 0:35 to 0:45 seconds.

Animations are based on keyframes. To animate a property, you need to create a keyframe and define the property values for that frame in time. In the previous example, we had just two Keyframes, for the start and end Scale values. Unity fills in-between values with a nice curve. You can insert additional Keyframes and edit the curve's shape. We will use these for the wafting nest.

A wafting nest

We are going to make the nest fall gently from the tree. We...

Animating other properties

Lighting is an important consideration in 3D animation and no less so for virtual reality storytelling. In our story, we want the lighting to start at night and progress through dawn to daylight. We'll do this by manipulating the Directional Light game object, a Point Light, and Skybox Material. The Directional Light game object has Intensity parameters we can animate directly. The skybox is defined with a material, so we are going to have to write a script to control its contribution to the lighting in the scene.

Animating lights

For dramatic effect, let's make the scene slowly fade from night to daytime. We will turn off Directional Light at the start and slowly increase its Intensity:

  1. SelectBlackbird Director inHierarchyand open theTimeline Editorwindow.
  2. Drag theDirectional Lightobject fromHierarchyonto the Timeline and select Animation Track.
  3. Press itsRecordbutton.
  4. Ensure...

Using Animation clips

For the next animation example, we'll get the egg to wobble and shake before it hatches. We will create a simple animation and make it loop for its duration. To illustrate, we'll make an Animation Clip of the WholeEgg object rattling and then add it to Timeline on an Animation Clip Track.

Shaking an egg

To create a new Animation Clip on the WholeEgg object, follow these steps:

  1. In Hierarchy, select the WholeEgg object (child of NestAndEgg).
  2. Open the Animation window (Window | Animation | Animation).
  3. You should see a message stating, To begin animating WholeEgg, create an Animation Clip, and a Create button.
  4. Press Create.
  5. When prompted for a filename, save it as EggShaker Animation.

We saw the Animation window earlier in this chapter. We're going to make a very short, 2-second animation that rotates the egg on the X and Z axes by manipulating the animation...

Using Animator Controllers

While recording animations as Timeline tracks is very convenient, it does have its limitations. These animations "live" in Timeline. But sometimes, you want to treat animations as assets in their own right. For example, you would use Animation Clips if you want an animation to loop repeatedly or transition between movements, blend their actions, and then apply the same set of animation curves to many different objects.

Animators were the standard way of managing Animation Clips in Unity before Timeline. It uses an Animator Component, an Animator Controller, and an Animation Clip. Fortunately, if you create a new Animation Clip on an object, Unity creates each of these items for you. But it's important to understand how they fit together.

Briefly, from the Unity Manual (https://docs.unity3d.com/Manual/animeditor-CreatingANewAnimationClip.html):

"To animate GameObjects in Unity, the object or objects need an...

Making the story interactive

So far, we've used Timeline to drive our entire VR story experience from start to finish. But in fact, Timelines are a playable asset like others in Unity. For example, if you select the Blackbird Director object and look in Inspector at its Playable Director component, you'll see it has a Play On Awake checkbox and that it's currently checked. What we're going to do now is not play on awake, but rather start playing on a user event, namely looking directly at the small tree for a few seconds. When the story ends, we'll make it reset itself.

Look to play

An interesting design pattern for VR application is to wait for the player to look at something before it starts animating or otherwise becomes active in the scene. After all, it'd be a shame for the user to not notice things and miss all the action! We'll demonstrate this idea in our project by waiting for the user to look at the small tree before...

Summary

In this chapter, we built an animated VR story. We began by deciding what we wanted to do by planning all the necessary details, including the timeline, music track, graphic assets, animation sequences, and lighting. We imported our assets and placed them in the scene. Then, we created a Timeline and roughly estimated when specific objects will be enabled and disabled using an Activation Track. Next, you learned how to animate several objects, including growing the tree and bird, floating the nest, and making the egg wobble. You also animated the lighting and learned how to animate game object parameters other than Transforms.

You also used Animation Clips and an Animator Controller in order to use animations imported from a third-party package, Living Birds. You wrote a control script that calls the Animator to tell the bird when to flap its wings for flying or landing. We then defined location markers to define where the bird will hop, fly, and land from one...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity 2020 Virtual Reality Projects - Third Edition
Published in: Jul 2020Publisher: PacktISBN-13: 9781839217333
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
Jonathan Linowes

Jonathan Linowes is a VR/AR enthusiast, Unity, and full stack developer, entrepreneur, certified Unity instructor, and the owner of Parkerhill XR Studio, an immersive media, applications, and game developer. Jonathan has a bachelor of fine arts degree from Syracuse University, a master of science degree from the MIT Media Lab, and has held technical leadership positions at Autodesk and other companies. He has authored multiple books on VR and AR published by Packt Publishing.
Read more about Jonathan Linowes