Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In last video, we talked about how to make local copies of animations for editing inside Unity. In this video, we'll look at setting up animation events, which allow animations to access the power of scripts by calling script functions as they play. So let's use this run-read/write animation that we created in the last video. I'll go ahead and apply that to the Third Person Controller script which is attached to our game character. So in the hierarchy, I'll just click on a game character. And down here in this Third Person Controller script, instead of the read-only run animation, I want to use this run-RW animation.
And I'll just click and drag to place that in Scripts. So right now this is just a blank script, so I'll double-click and we can make some edits here. So right now, we have a default function called the Update function. So that's a function that automatically comes up in any script and there we could place scripting language to control things that happen every frame. In this case though, we're just going to make a very simple function. So I'm just going to type function and I'll title it Running.
And it's actually a lot of fun. You can do some really powerful things with scripts though Unity. So let's go ahead and save this. And we can quit out of our text editor. Whoops! I'm getting an error down here already. So let me just double-click one more time. I forgot my semicolon here. Okay. So I want to take the script, left-click, drag over to the hierarchy, and just apply it to our game character. So now let's click on root again and go in and add an animation event to our read/write animation.
So I'll bring up the animation window by clicking Window > Animation and we'll edit our run-RW animation. So in the timeline here, right below there's this little track and that holds animation events. So I can drag the time slider out. Let's say halfway through I want to add an animation event here and I can just click Add Event, so I've got an event here. And right now, there's no function selected. So basically an animation event is just going to talk to any of the scripts that are attached to the game object that you're animating.
So to edit this, I can just double-click and it's going to ask me to pick a function. So right now I get a big list. So these are all the functions from all the scripts that are attached to my game character right now. What I want to pick is this Running function. So as this animation plays, when it hits this point, it's going to call that Running function that we just set up in our script and it's going to print that text to the console. Okay, so I'll close out of the animation window.
And I'll go to Window. It'll bring up the console so we can actually see the text that we're going to get when we play. And I'll just click this tab here, just drag it right down to the Project window. So one thing about the Unity interface is it's really easy to customize, so you can reposition and drag around windows as you need to. So I'll go ahead and switch to my Game view. Let's go ahead and play. So I'm going to hold down Shift+W and I should see in the console now my text is appearing as that running animation plays over and over again.
So that's a really simple example of how to get an animation to talk to a script. Now obviously, you would probably want to do something more elaborate with your script. You could use it to control other game objects that are in your scene, maybe turn a light on or off, something like that. Really, the sky is the limit and it's just a matter of getting familiar with scripting and getting familiar with Unity.
Get unlimited access to all courses for just $25/month.Become a member
107 Video lessons · 34613 Viewers
94 Video lessons · 24145 Viewers
100 Video lessons · 4823 Viewers
83 Video lessons · 9745 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.