Join Sue Blackman for an in-depth discussion in this video Triggering keyframe animations, part of Unity 3D: 3.5 Essential Training.
Now let's see about triggering a new animation clip. In the Game Scripts folder, let's create a new script and name it PowerUp. Right-click, Create, Javascript, and we'll call this one PowerUp, and let's go ahead and open it. Double-click to bring up the script editor. Let's start by making a variable for the animation clip, and we're going to call it clip, var clip of type AnimationClip.
And now, let's turn the Update into an OnTriggerEnter function, and inside it we're going to add animation.Play, and Play is a function, so it's capitalized, and it gets the open and closing parentheses. So without specifying an animation name, Unity will play the default clip, but we need it to play the other clip, the heart score clip. We'll feed in the correct clip, but we need to use its name as the function argument.
So our variable clip, which is the actual clip, but all we really need is its named property. Let's go ahead and save the script and head back to the editor. And we need to add the new script to the Heart. We will select the Heart, find our new script, and we need to bring in the animation clip. We can click on the browser and here it is, heart score.
Let's click Play and jump the first-person controller into it. Remember, spacebar to jump, click Play and test by jumping the first-person controller into it with the spacebar. It works. So let's Exit Play mode. If you need to adjust any of the timing, you can just select the heart, open the Animation view, select the heart score clip, and make adjustments that you need. Before we continue with the PowerUp script, let's go ahead and load a little package I've made for us, it's called ScoreKeeper.
It's just an empty game object with a simple script. So right-click, Import Package, Custom Package, and from our Asset folder, we'll bring in ScoreKeeper. Clicking Open and then Import. It should turn up in our Prefabs folder, and there it is, and we'll go ahead and drag it into the scene.
Let's take a quick look at the ScoreKeeper script, double-click it, and you can see it's really simple. It has a variable named score which is initialized at 0, and then it has a function called UpdateScore. It has a local variable called points that we will be passing to it, and that increments the score with the number of points we feed it. Let's head back to our PowerUp script now in the other tab, so we're going to need a couple of new variables. I'm going to go out to the Snippets file and Copy them.
Exercise Files10/10-04/Code_Snippets, and we need these right here, pointValue and ScoreKeeper. Right-click and copy, back into the Script editor, and we'll put them right beneath our first variable. The pointValue is the value for each of our PowerUps and the ScoreKeeper is the game object that we just imported into our scene. He's keeping track of the overall score.
Let's go ahead and get the next bit of code, and for the ScoreKeeper--I'm going to right-click and copy--we're going to send a message to its UpdateScore function to tell us what point value it should update the score by, and we can put this one underneath the Animation, paste, and then we may as well go ahead and put some sound effects in there. And we're going to var a sound effects variable to hold an AudioClip, right-click, Copy, add that variable up here and grab our final line.
Right-click, Copy, and we'll Play our sound effects right here. Tab over, and I'm going to be neat and tidy and give myself one open line. Okay, so we're ready to save our script. The first thing we need to do is make sure we put our PowerUp script on the heart. On my console, it's giving me an error. I need to go back to my Script editor and make sure I capitalize GameObject. That will make it happy. Now I'll click Save, and now we can go back to the editor.
In the Inspector, with our heart selected we now need to fill in our two new parameters. ScoreKeeper, we'll drag him into that parameter. And for the Sound Effects, I'm going to click on the browser and choose Ding. The heart and the key Prefabs already have an audio source with the Rolloffs set to Linear, so we should be able to hear it easily. Let's go ahead and click Play and see what happens now, W, spacebar, and there we go.
You'll notice that when we trigger the new clip to Play, the first one is automatically turned off. Let's Exit Play mode, and if your first-person controller is falling off the platforms too often, you can go ahead and activate the two invisible walls again.
Author
Released
7/19/2012- Understanding game and level design theory
- Organizing your project in Unity
- Creating and transforming objects
- Setting up the geometry
- Painting in terrain, textures, and trees
- Adjusting the render settings
- Importing terrains
- Creating a first-person controller
- Building projectiles with JavaScript
- Creating materials and shaders
- Lighting the game
- Working with cameras and multiple views
- Animating characters and assets
- Creating fire with particle systems
- Managing the GUI (graphical user interface)
Skill Level Beginner
Duration
Views
Related Courses
-
Game Character Creation in Maya
with Chris Reilly2h 58m Intermediate
-
Introduction
-
Welcome55s
-
Unity basics1m 8s
-
Using the exercise files1m 29s
-
-
1. Exploring Unity 3D
-
Exploring the interface4m 28s
-
Organizing your project8m 12s
-
Exploring Scene view3m 14s
-
Navigating Scene view2m 52s
-
Creating objects7m 38s
-
Transforming objects8m 16s
-
-
2. Exploring the Terrain Editor
-
Painting the topography3m 56s
-
Painting trees and forests4m 56s
-
Painting detail meshes5m 4s
-
Adjusting terrain settings3m 23s
-
3. Creating the Environment
-
Publishing project settings3m 37s
-
4. Introducing Unity Scripting
-
Exploring Unity scripting3m 34s
-
Using variables11m 46s
-
Discovering functions4m 10s
-
Looking at function syntax3m 44s
-
Printing to the console8m 28s
-
Scripting basic functions7m 12s
-
Combining transforms5m 27s
-
Setting transforms8m 34s
-
-
5. In-Game Scripting
-
Raycasting6m 56s
-
Building timers7m 8s
-
Creating a simple projectile13m 16s
-
Refining the projectile5m 32s
-
Sending messages3m 38s
-
Reacting to messages4m 50s
-
Juggling Play and Edit modes7m 33s
-
Suppressing input8m 2s
-
6. Working with GameObjects and Components
-
Creating GameObjects6m 14s
-
Understanding components5m 3s
-
Using colliders for barriers7m 15s
-
Using colliders for triggers4m 28s
-
Exploring physics7m 15s
-
Making cloth9m 56s
-
Working with wind zones7m 50s
-
Using an audio source6m 14s
-
Creating a sound zone5m 34s
-
Adding audio effects6m 31s
-
-
7. Exploring Prefabs
-
Creating and reusing prefabs8m 11s
-
Using prefabs with arrays5m 28s
-
Creating particle systems6m 58s
-
Refining particle systems5m 30s
-
Combining particle systems7m 10s
-
Upgrading weapons6m 15s
-
Exploring water effects7m 53s
-
-
8. Using Imported Assets
-
Importing static objects12m 36s
-
Manipulating textures5m 42s
-
Handling multiple materials5m 42s
-
Animating UVs8m 27s
-
Tracking objects with LookAt5m 45s
-
-
9. Understanding Lighting
-
Investigating ambient light2m 21s
-
Looking at light types3m 13s
-
Exploring shadows5m 23s
-
Baking lighting with Beast8m 16s
-
Experimenting with Beast7m 42s
-
Baking the game scene4m 20s
-
Creating lighting effects6m 24s
-
Adding a flashlight9m 20s
-
-
10. Keyframing Animation
-
Using the Animation view9m 53s
-
Animating transparency7m 12s
-
Keyframing events6m 28s
-
Avoiding keyframing pitfalls5m 27s
-
11. Animating Skinned Meshes and Controlling Characters
-
Importing skinned meshes9m 42s
-
Dynamic parenting6m 44s
-
-
12. Working with Cameras and Layers
-
Introducing cameras4m 53s
-
Managing multiple levels7m 34s
-
-
13. Creating Game GUIs
-
Working with the Unity GUI10m 31s
-
Using GUI skins6m 30s
-
Introducing fonts and styles5m 56s
-
Scripting in-game menus3m 44s
-
Hooking up an in-game menu5m 20s
-
Creating custom cursors8m 42s
-
-
14. Extra Techniques and Features
-
Creating fade transitions5m 34s
-
Setting AI pathfinding9m 56s
-
-
Conclusion
-
What's next1m 13s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Triggering keyframe animations