Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
[MUSIC] 2
2
Hello, again. 3
3
In this video, we're gonna be using animation events to control the problem we 4
4
had of the lizard walking at one speed, 5
5
even when they're supposed to be jumping and now sliding across the screen, and 6
6
having a little bit of fun to add more depth to our control over our animation. 7
7
So let's jump in and get started. 8
8
Okay, what are animation events, and where do we find them? 9
9
Let me show you a slide. 10
10
And to add an event, you see our animation window here, 11
11
there's the little button that's the Add event button. 12
12
That will add an event on the timeline, wherever your timeline 13
13
slider bar thingy is, and you can add a whole bunch of events. 14
14
This will call a function that we have in our script, so 15
15
let's put this into practice. 16
16
Let's jump over and find Lizard Walk. 17
17
And from Lizard Walk, I wanna find my timeline. 18
18
Sometimes you need to click around a little bit, I find, to jog it into place. 19
19
Cool, we've got it there. 20
20
Here is the event button, I'll click on that. 21
21
That's added an event right at the front of my timeline cuz that's where my slider, 22
22
what's this thing called? 23
23
The timeline line indicator doobie whatsit, I think is the technical. 24
24
Go look that up in the Unity docs, I think that's what you'll find, 25
25
the doobie whatsit. 26
26
Okay, so I've added one event here. 27
27
If I click over in the Inspector, you can see that we have Function, 28
28
Float, Int, String, Object. 29
29
This means that we can call a function 30
30
that we need to have somewhere in our script. 31
31
And the way we've structured this is when Lizard Walk, as an animation, 32
32
is placed on a lizard, and we have our lizard prefab that it's on at the moment. 33
33
Then another class that is also on that game object, 34
34
also on Lizard, we will be able to call. 35
35
So let's create a function here called SetMovementSpeed, SetMovementSpeed. 36
36
So on Lizard Walk, we want our lizard to, let's be a little bit crazy here. 37
37
We'll set this to 10, just to make sure this is working. 38
38
That should be fun and crazy. 39
39
So the very instant the lizard is created, it'll be 10. 40
40
You know what, I'm gonna grab this and drag it, cuz you can move it around. 41
41
I'm gonna drag it into the middle of my timeline so that we'll go along, go along, 42
42
and then we get at this point [SOUND], it's gonna go to 10, 43
43
which will be super exciting. 44
44
Okeydokey, so let's go and open up our script. 45
45
I will open up the Attacker script. 46
46
And working on the Attacker script, 47
47
what I'm gonna do first of all is change this notion of a serialized walkSpeed. 48
48
So we want more than walkSpeed, we want our lizard, or 49
49
whichever attacker we're referring to, to have a notion of movement speed, 50
50
whether that's the jump or the walk. 51
51
We wanna have this movement speed so that we can have different amounts of speed. 52
52
So I'm gonna just get rid of that and 53
53
change it to a float, doesn't need to be serialized. 54
54
What should we call this, my friends? 55
55
Let's call it currentSpeed, so 56
56
we're very clear that's currently how fast we're going. 57
57
We'll initialize it to 1f, okay. 58
58
And you know what, I'm not gonna need my Start for now, 59
59
we'll bring it back if we need it. 60
60
I don't need to know that updates are called once per frame. 61
61
I think we know that by now. 62
62
Instead of walkSpeed, 63
63
I'll change this to currentSpeed in my method here on the update. 64
64
We're saying, move with current speed. 65
65
Now, this is where we need to do a little bit of magic. 66
66
We need to have our animation, when it comes along, plays [SOUND]. 67
67
When it gets to this frame on this animation, it is going to call, 68
68
let's click on that. 69
69
It's going to call what? 70
70
It's gonna call SetMovementSpeed, so 71
71
we need to create SetMovementSpeed as a public function. 72
72
This is where I have a challenge for you. 73
73
I want you to create a public method called SetMovementSpeed(), and 74
74
we will require that method to pass in a float called speed. 75
75
So it'll have the parameter, which is a float, what we'll call speed. 76
76
So hopefully that makes sense, 77
77
a little bit of rejogging your memory here on how that works. 78
78
So I need to pass in a float called speed, and then I'd like for you to update 79
79
the attacker's current speed so that it is now the speed value that was passed in. 80
80
So I'm gonna pass in speed from. 81
81
Where are we passing from? 82
82
From over in here, from our animation event. 83
83
And it's gonna be a float, and then we want to, in our script, in our Attacker 84
84
script, to then update the current speed to be the speed that was just passed in. 85
85
So hopefully that makes sense. 86
86
If not, sit there and then ponder it for a while, 87
87
see if you can get your brain around it. 88
88
Give it your best shot, this is how you learn the best, 89
89
is taking on these challenges. 90
90
And I'll see you back in here in a moment for the solution. 91
91
Okay, how did you get along with that? 92
92
I'm pretty sure that you nailed it. 93
93
But just in case, let's go through this together. 94
94
Back into my script, what I need to do is create a public method. 95
95
So public, there's no return type, so it's void, 96
96
and we're calling this SetMovementSpeed, and I just wanna flag. 97
97
I'll probably say this a few times, 98
98
but we're referencing something that is a string reference in essence. 99
99
So if I was to go and change the name of my method in here, 100
100
it's not going to change the name that we're seeing on our animation event, so 101
101
it is a little bit of a clunky approach for doing this. 102
102
It's not perfect, there are some shortfalls. 103
103
But it gives us a gigantic amount of 104
104
flexibility within our animation to do super cool things. 105
105
So I have SetMovementSpeed, probably copy that and then paste it in here so 106
106
we know we've got it right. 107
107
And we need to pass in something, so that'll be a float. 108
108
And we'll call this speed. 109
109
And then within our method, 110
110
what do we want to do when something calls SetMovementSpeed? 111
111
Well, we need to change this currentSpeed to be whatever has been passed in. 112
112
So we can just say currentSpeed is now assigned as speed. 113
113
So if we go on passing 10, which we're doing here, we're passing in 10, 114
114
then we'll be multiplying by 10 in the currentSpeed. 115
115
Okay, that looks pretty sharp, I reckon. 116
116
Save that, back over into Unity. 117
117
Now, let's see what it's going to do, click on Play Okay, 118
118
our guy should jump, walk, walk, walk, and then [SOUND]. 119
119
Takes off super fast when it gets to this point where it's going 10, that's awesome. 120
120
Now, just a little bit of playing around here, having a little bit of fun. 121
121
You could, at this point of your timeline, say, walk normally, and then stop. 122
122
And that is as simple as saying, add a new animation event, 123
123
find our name, which we know is SetMovementSpeed. 124
124
If you have any issues with this, the first thing to do is make sure you're 125
125
spelling it the same way there as you're spelling over in here. 126
126
And like I said, it's good to copy and paste to make sure it's all good. 127
127
I'm gonna set this at 0, Save. 128
128
As simple as that, click on Play. 129
129
Walk, walk, jump in first. 130
130
Jump, boink, walk, walk, walk, stop, and then [SOUND] take off. 131
131
And then we could add another one down here that stops it or starts it. 132
132
So this is a great way to get tons of control, not just over how your frames 133
133
are playing with the animation, but also the other things that you want your thing, 134
134
your character, to be doing on screen. 135
135
We could also get in here and manipulate our frame. 136
136
So I could stretch this out, and here, if I wanted to have the character walk 137
137
slowly, I grab that, I drag it, then we can have our frames represent that. 138
138
But I would need to grab our animation event and move that back as well. 139
139
So you can do a lot of monkeying around, you can play around in here. 140
140
Let's go and implement our sensible solution now. 141
141
So I'm going to shift this all the way back to the start, 142
142
if I can grab it properly, all the way back to the start, delete this one. 143
143
One more time, Rick, learn how to use a mouse, would you? 144
144
Okay, we're gonna set this to 1. 145
145
That's the speed we want him to be walking when he is walking. 146
146
Find the other animation, which was, a lot of the reason we were doing this 147
147
is cuz when the character was jumping, it was moving at one speed and sliding. 148
148
So we add an animation event at the start here. 149
149
We paste in our SetMovementSpeed, and change our float to 0, 150
150
which it is already by default. 151
151
That is excellent. 152
152
Save that up, now see if this works. 153
153
We want our character to jump, not move, and then start walking. 154
154
So jump, stationary, and then start walking, awesome. 155
155
And that is because, in our Animator, we see that our character, 156
156
where are you, Animator Controller, here we go. 157
157
We see that our Jump is first to happen. 158
158
It's going at 0 speed, and then we transition into Walk. 159
159
And the very first frame of Walk, we are telling it, through an animation event, 160
160
where is my walk animation event, we're telling it to move at speed of 1. 161
161
Okay, cool, so really super useful technique. 162
162
This is something you can use in fighting games, for 163
163
example, if you wanna have a character reaction. 164
164
So right in the middle of the frame is where they, 165
165
it looks like they're punching the other character, or punching the enemy, 166
166
then that is the frame that you can trigger the damage. 167
167
So if you've got this big long swing, 168
168
you're not having the code trying to guess where the actual impact took place. 169
169
You can do it right down to the exact frame of this is exactly where the bullet 170
170
strikes, or the punch hits, or the character jumps. 171
171
And you can be triggering all manner of exciting things, 172
172
sound effects, changes in variables, etc. 173
173
So there it is, there's the technique of using the animation event to 174
174
add more depth to our animations. 175
175
Great work, and I'll see you in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.