Table des matières
-
Lessons
Before engaging this section, you must complete the previous section - Posting Events using Wwise-Types. |
To handle animations in Unity, you can use Animation clips. These clips can either be animations you've created in an external program like Maya or 3Ds Max, or simply modifications to object(s) in Unity. Let's take a look at how the Adventurer was created.
Her model was created in Maya, and numerous scripts were attached to her game object so she can Move, Attack, and so on. But to really make her look alive, her arm needs to swing when she is attacking a creature and her legs need to take a step forward when pressing the forward button.
Now, what about sound? In Wwise you will see a collection of high quality footstep sounds in a Random Container, but how do you make sure to play the sound exactly when the Adventurers foot is touching the ground? For this we can create an Animation Event, a Unity feature connecting a point in time within an animation to a function in a script. First you select a specific time in an Animation and then you specify a function in a script, in which you could post an Event. In the following steps, you'll change the previously created PostWwiseEvent to posting the player footstep sound, and modify the Start function to be called from an Animation Event. Following that we will look at the Animation Editor and call our new function directly from an Animation Event.
-
In Unity, go to Audiokinetic > Certification > 301 > Lesson 4 and select Posting Events from Animations.
-
In the Hierarchy, select the Player game object.
Let's add a PostWwiseEvent script to the Player game object - a script you created in a previous section and will be using in Lesson 5 as well.
-
In the Inspector, click Add Component and select PostWwiseEvent.
If you are not able to see this script in the project, you should return to the previous section, Creating a Wwise-Type Event property.
-
Click the MyEvent property drawer and select the Player_Footstep Event.
-
Double-click the PostWwiseEvent to open the script.
Visual Studio (or your default code editor) will open and here you'll find the previously created PostWwiseEvent script, where the MyEvent.Post() function call will call the Event selected in the MyEvent property once entering play mode.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PostWwiseEvent : MonoBehaviour { public AK.Wwise.Event MyEvent; // Use this for initialization. void Start () { MyEvent.Post(gameObject); } // Update is called once per frame. void Update () { } }
Currently, as the MyEvent.Post() function call is in the Start() function, the Event selected in the MyEvent property will be posted when starting the game. But as we would like to call the function from an Animation Event, we can simply give the function a custom name so that Unity will not automatically call it on executing the game.
The name of the function is important in reference to these lesson steps, but not for using Animation Events in Unity. You can easily have an alternative function name like 'PostFootstepEvent', but be aware that if the name is identical to a function used in the Unity API, another process might also call it.
-
Replace Start with PlayFootstepSound.
public class PostWwiseEvent : MonoBehaviour { public AK.Wwise.Event MyEvent; // Use this for initialization. void PlayFootstepSound() { MyEvent.Post(gameObject); }
Previously, the script's Start() function would be called on entering Play mode, but now that you have renamed it, Unity will no longer automatically call what is in that function when the Scene loads. Before we turn to our Animation Event, you need to allow Unity to see this newly named function by placing the public Access Modifier before the function is declared.
-
Insert public in front of void PlayFootstepSound().
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PostWwiseEvent : MonoBehaviour { public AK.Wwise.Event MyEvent; // Use this for initialization. public void PlayFootstepSound() { MyEvent.Post(gameObject); } }
Now that you've made the PlayFootstepSound() function public, our script is complete.
-
Press CTRL+S on Windows or CMD+S on Mac to save the script.
Let's now return to Unity and create an Animation Event. As you need to add an Animation Event to the Player, let's first select the Player game object.
-
In the Hierarchy, ensure that the Player game object is selected.
To add Animation Events, you will need to open the Animation Window.
-
In the Unity menu, go to Window and select Animation.
The Animation Window will now appear, which is where the settings of the animations are modified. If this is the first time you open it, it might appear in a separate window. Before taking a closer look, let's fit the window with the rest of the Layout.
-
Drag the Animation window by its tab onto the right side of the Unity Project view.
With the Player game object selected in the Hierarchy, the Animation Window will now show you the Player_Idle_wHammer Animation Clip.
An Animation clip is a way to store a sequence of changes in properties. All properties are listed in the left window pane. On the right, you will see a series of points at which the property is changed. These points are referred to as keyframes. Above the keyframes and below the Timeline (topmost bar displaying time), you will find the Event line, which is where Animation Events are added. When the white Time Cursor reaches an Animation Event, it will call the assigned function, so you can make sure an action is called at a specific time in the Animation clip. You will now be asked to call the PlayFootstepSound() function at the moment where the Player's foot reaches the ground, and, for the sake of demonstration, let's use the Player_Sprint Animation clip."
-
To see all Animation clips used by the Player, open the top-left list menu.
Here you'll find all the Animation clips used by the Player's animation system, managed by the Animator component.
-
In the top-left list, select Player_Sprint.
To the left you see which components are being changed during the animation, and on the right you'll see the Timeline with the Event line below it.
Leftmost in the Timeline, notice a white Timeline Cursor, drawn from top to bottom. This line shows the current time in the Animation, and as you change its position in the Timeline, by either dragging or clicking, the Adventurer will adjust her animation to the corresponding time in her animation clip in the Scene view.
Notice that the Adventurer is set to the Player_Sprint Animation clip. By dragging the Time Cursor from side to side, you will see how the Adventurer turer in turn places her left and right foot on the ground.
-
In the Timeline, drag the Timeline Cursor back and forth.
If your view is cluttered with different symbols and text, you can remove the majority of it in the Scene view's Gizmos tab. n
-
In the Scene view, go to the Gizmos tab and disable 3D Icons.
Next, you will locate the exact spot in the animation where you want the sound to begin.
-
Drag the Timeline Cursor to where the Adventurer is placing her right foot on the ground (0:05).
Here you will add an Animation Event, which you will later assign to the PlayFootstepSound() function.
-
Right-click at the Time Cursor (0:05) in the Event line and select Add Animation Event.
Notice the new Animation Event marker in the Event line.
To make sure the timing is correct, double check that the Animation Event marker is exactly at 0:05, or else it will not correspond to what you are seeing in the Scene view. Next, take a look in the Inspector.
Here you can add the function that should be called at that point in the timeline. This is possible due to the public access modifier you added earlier in this section.
Animation Events can only be connected to functions located on the same game object as the Animator component. If you want to connect to a script on another game object, you need to write an additional script to forward the function calls.
-
Click the Function property drawer and select PostWwiseEvent > Methods > PlayFootstepSound().
With your function linked to the Animation Event Marker, you are ready to test out your sound.
The Function list is quite comprehensive, but you can jump to the name of a function by typing the first letter, which in this case would be 'P'.
-
Enter Play mode as described in Lesson 1, then hold down Shift while pressing W to sprint.
You've now added the right foot. Next, you will add the left foot. Exit Play mode and return to the Animation window.
-
Drag the Time Cursor until you see the left foot touching the ground (0:15).
-
Right-click inside the Event line and select Add Animation Event.
Alternatively, you can add Animation Events by clicking the Add Event button to the left of the Event line. This will add an Animation Event at the position of the Timeline Cursor.
-
In the Inspector, select the PlayFootstepSound() function.
-
Click Play and sprint around the scene.
Sprint around in the Training Area and listen to the footstep sounds. Notice how the footstep sounds are fluidly following the Player's animation. By connecting a sound directly to an animation, you have ensured that the Footsteps are only being played when the Player is performing the proper animation. Following the same procedure as in this exercise, you can incorporate footstep sounds into your walk animations, your attack animations and much more.
Next, you will save the changes to the Player Prefab, so it's ready to be used in Lesson 5 as well.
-
Press ESC to open the WAG menu and click Play again to exit Play mode.
-
Select the Player game object and in the Inspector, click Overrides > Apply All.
Now all Player game objects in the project will have the same footstep sound behaviour linked to the Animation.