Join Sue Blackman for an in-depth discussion in this video Triggering keyframe animations, part of Unity 3D 3.5 Essential Training.
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.
- 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
- 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
Creating Urban Game Environments in 3ds Maxwith Adam Crespi5h 54m Intermediate
1. Exploring Unity 3D
2. Exploring the Terrain Editor
3. Creating the Environment
Publishing project settings3m 37s
4. Introducing Unity Scripting
5. In-Game Scripting
6. Working with GameObjects and Components
7. Exploring Prefabs
8. Using Imported Assets
9. Understanding Lighting
10. Keyframing Animation
11. Animating Skinned Meshes and Controlling Characters
12. Working with Cameras and Layers
13. Creating Game GUIs
14. Extra Techniques and Features
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.Cancel
Take 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.