Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:00,000 --> 00:00:04,214
[MUSIC]
2
2
00:00:04,214 --> 00:00:04,720
Hello, again.
3
3
00:00:04,720 --> 00:00:08,650
In this video, we're gonna be using
animation events to control the problem we
4
4
00:00:08,650 --> 00:00:11,360
had of the lizard walking at one speed,
5
5
00:00:11,360 --> 00:00:15,160
even when they're supposed to be jumping
and now sliding across the screen, and
6
6
00:00:15,160 --> 00:00:20,160
having a little bit of fun to add more
depth to our control over our animation.
7
7
00:00:20,160 --> 00:00:23,530
So let's jump in and get started.
8
8
00:00:23,530 --> 00:00:26,670
Okay, what are animation events,
and where do we find them?
9
9
00:00:26,670 --> 00:00:28,410
Let me show you a slide.
10
10
00:00:28,410 --> 00:00:31,250
And to add an event,
you see our animation window here,
11
11
00:00:31,250 --> 00:00:34,500
there's the little button
that's the Add event button.
12
12
00:00:34,500 --> 00:00:39,410
That will add an event on the timeline,
wherever your timeline
13
13
00:00:39,410 --> 00:00:44,520
slider bar thingy is, and
you can add a whole bunch of events.
14
14
00:00:44,520 --> 00:00:47,950
This will call a function that
we have in our script, so
15
15
00:00:47,950 --> 00:00:49,270
let's put this into practice.
16
16
00:00:49,270 --> 00:00:52,520
Let's jump over and find Lizard Walk.
17
17
00:00:52,520 --> 00:00:55,000
And from Lizard Walk,
I wanna find my timeline.
18
18
00:00:55,000 --> 00:00:59,040
Sometimes you need to click around a
little bit, I find, to jog it into place.
19
19
00:00:59,040 --> 00:01:00,300
Cool, we've got it there.
20
20
00:01:00,300 --> 00:01:02,950
Here is the event button,
I'll click on that.
21
21
00:01:02,950 --> 00:01:07,200
That's added an event right at the front
of my timeline cuz that's where my slider,
22
22
00:01:07,200 --> 00:01:08,340
what's this thing called?
23
23
00:01:08,340 --> 00:01:12,651
The timeline line indicator doobie
whatsit, I think is the technical.
24
24
00:01:12,651 --> 00:01:16,115
Go look that up in the Unity docs,
I think that's what you'll find,
25
25
00:01:16,115 --> 00:01:17,230
the doobie whatsit.
26
26
00:01:17,230 --> 00:01:18,970
Okay, so I've added one event here.
27
27
00:01:18,970 --> 00:01:21,750
If I click over in the Inspector,
you can see that we have Function,
28
28
00:01:21,750 --> 00:01:23,510
Float, Int, String, Object.
29
29
00:01:23,510 --> 00:01:26,220
This means that we can call a function
30
30
00:01:26,220 --> 00:01:28,550
that we need to have
somewhere in our script.
31
31
00:01:28,550 --> 00:01:32,700
And the way we've structured this is
when Lizard Walk, as an animation,
32
32
00:01:32,700 --> 00:01:36,648
is placed on a lizard, and we have our
lizard prefab that it's on at the moment.
33
33
00:01:36,648 --> 00:01:40,480
Then another class that is
also on that game object,
34
34
00:01:40,480 --> 00:01:43,950
also on Lizard, we will be able to call.
35
35
00:01:43,950 --> 00:01:50,450
So let's create a function here called
SetMovementSpeed, SetMovementSpeed.
36
36
00:01:50,450 --> 00:01:55,400
So on Lizard Walk, we want our lizard to,
let's be a little bit crazy here.
37
37
00:01:55,400 --> 00:02:00,080
We'll set this to 10,
just to make sure this is working.
38
38
00:02:00,080 --> 00:02:01,730
That should be fun and crazy.
39
39
00:02:01,730 --> 00:02:04,740
So the very instant the lizard is created,
it'll be 10.
40
40
00:02:04,740 --> 00:02:08,688
You know what, I'm gonna grab this and
drag it, cuz you can move it around.
41
41
00:02:08,688 --> 00:02:13,003
I'm gonna drag it into the middle of my
timeline so that we'll go along, go along,
42
42
00:02:13,003 --> 00:02:16,084
and then we get at this point [SOUND],
it's gonna go to 10,
43
43
00:02:16,084 --> 00:02:17,705
which will be super exciting.
44
44
00:02:17,705 --> 00:02:21,010
Okeydokey, so let's go and
open up our script.
45
45
00:02:21,010 --> 00:02:23,711
I will open up the Attacker script.
46
46
00:02:23,711 --> 00:02:25,404
And working on the Attacker script,
47
47
00:02:25,404 --> 00:02:29,300
what I'm gonna do first of all is change
this notion of a serialized walkSpeed.
48
48
00:02:29,300 --> 00:02:31,978
So we want more than walkSpeed,
we want our lizard, or
49
49
00:02:31,978 --> 00:02:35,822
whichever attacker we're referring to,
to have a notion of movement speed,
50
50
00:02:35,822 --> 00:02:38,030
whether that's the jump or the walk.
51
51
00:02:38,030 --> 00:02:41,585
We wanna have this movement speed so that
we can have different amounts of speed.
52
52
00:02:41,585 --> 00:02:43,590
So I'm gonna just get rid of that and
53
53
00:02:43,590 --> 00:02:47,580
change it to a float,
doesn't need to be serialized.
54
54
00:02:47,580 --> 00:02:48,910
What should we call this, my friends?
55
55
00:02:48,910 --> 00:02:51,080
Let's call it currentSpeed, so
56
56
00:02:51,080 --> 00:02:54,020
we're very clear that's
currently how fast we're going.
57
57
00:02:54,020 --> 00:02:56,950
We'll initialize it to 1f, okay.
58
58
00:02:56,950 --> 00:02:59,130
And you know what,
I'm not gonna need my Start for now,
59
59
00:02:59,130 --> 00:03:00,480
we'll bring it back if we need it.
60
60
00:03:00,480 --> 00:03:02,970
I don't need to know that updates
are called once per frame.
61
61
00:03:02,970 --> 00:03:04,770
I think we know that by now.
62
62
00:03:04,770 --> 00:03:05,755
Instead of walkSpeed,
63
63
00:03:05,755 --> 00:03:10,660
I'll change this to currentSpeed
in my method here on the update.
64
64
00:03:10,660 --> 00:03:12,480
We're saying, move with current speed.
65
65
00:03:12,480 --> 00:03:15,440
Now, this is where we need
to do a little bit of magic.
66
66
00:03:15,440 --> 00:03:20,241
We need to have our animation,
when it comes along, plays [SOUND].
67
67
00:03:20,241 --> 00:03:23,990
When it gets to this frame on this
animation, it is going to call,
68
68
00:03:23,990 --> 00:03:25,300
let's click on that.
69
69
00:03:25,300 --> 00:03:27,008
It's going to call what?
70
70
00:03:27,008 --> 00:03:28,790
It's gonna call SetMovementSpeed, so
71
71
00:03:28,790 --> 00:03:33,380
we need to create SetMovementSpeed
as a public function.
72
72
00:03:33,380 --> 00:03:36,220
This is where I have a challenge for you.
73
73
00:03:36,220 --> 00:03:41,207
I want you to create a public method
called SetMovementSpeed(), and
74
74
00:03:41,207 --> 00:03:45,370
we will require that method to
pass in a float called speed.
75
75
00:03:45,370 --> 00:03:49,890
So it'll have the parameter,
which is a float, what we'll call speed.
76
76
00:03:49,890 --> 00:03:50,970
So hopefully that makes sense,
77
77
00:03:50,970 --> 00:03:55,470
a little bit of rejogging your
memory here on how that works.
78
78
00:03:55,470 --> 00:03:58,780
So I need to pass in a float called speed,
and then I'd like for you to update
79
79
00:03:58,780 --> 00:04:05,080
the attacker's current speed so that it is
now the speed value that was passed in.
80
80
00:04:05,080 --> 00:04:08,760
So I'm gonna pass in speed from.
81
81
00:04:08,760 --> 00:04:10,280
Where are we passing from?
82
82
00:04:10,280 --> 00:04:12,590
From over in here,
from our animation event.
83
83
00:04:12,590 --> 00:04:16,300
And it's gonna be a float, and then we
want to, in our script, in our Attacker
84
84
00:04:16,300 --> 00:04:21,780
script, to then update the current speed
to be the speed that was just passed in.
85
85
00:04:21,780 --> 00:04:22,730
So hopefully that makes sense.
86
86
00:04:22,730 --> 00:04:25,280
If not, sit there and
then ponder it for a while,
87
87
00:04:25,280 --> 00:04:26,620
see if you can get your brain around it.
88
88
00:04:26,620 --> 00:04:28,701
Give it your best shot,
this is how you learn the best,
89
89
00:04:28,701 --> 00:04:29,939
is taking on these challenges.
90
90
00:04:29,939 --> 00:04:32,554
And I'll see you back in here
in a moment for the solution.
91
91
00:04:35,259 --> 00:04:36,920
Okay, how did you get along with that?
92
92
00:04:36,920 --> 00:04:38,640
I'm pretty sure that you nailed it.
93
93
00:04:38,640 --> 00:04:41,070
But just in case,
let's go through this together.
94
94
00:04:41,070 --> 00:04:43,980
Back into my script, what I need
to do is create a public method.
95
95
00:04:43,980 --> 00:04:47,876
So public, there's no return type,
so it's void,
96
96
00:04:47,876 --> 00:04:52,820
and we're calling this SetMovementSpeed,
and I just wanna flag.
97
97
00:04:52,820 --> 00:04:54,600
I'll probably say this a few times,
98
98
00:04:54,600 --> 00:04:58,760
but we're referencing something that
is a string reference in essence.
99
99
00:04:58,760 --> 00:05:02,465
So if I was to go and
change the name of my method in here,
100
100
00:05:02,465 --> 00:05:06,425
it's not going to change the name that
we're seeing on our animation event, so
101
101
00:05:06,425 --> 00:05:09,525
it is a little bit of a clunky
approach for doing this.
102
102
00:05:09,525 --> 00:05:12,215
It's not perfect,
there are some shortfalls.
103
103
00:05:12,215 --> 00:05:15,455
But it gives us a gigantic amount of
104
104
00:05:15,455 --> 00:05:19,521
flexibility within our animation
to do super cool things.
105
105
00:05:19,521 --> 00:05:22,685
So I have SetMovementSpeed, probably
copy that and then paste it in here so
106
106
00:05:22,685 --> 00:05:23,775
we know we've got it right.
107
107
00:05:23,775 --> 00:05:26,300
And we need to pass in something,
so that'll be a float.
108
108
00:05:26,300 --> 00:05:27,762
And we'll call this speed.
109
109
00:05:27,762 --> 00:05:29,381
And then within our method,
110
110
00:05:29,381 --> 00:05:33,150
what do we want to do when
something calls SetMovementSpeed?
111
111
00:05:33,150 --> 00:05:37,570
Well, we need to change this currentSpeed
to be whatever has been passed in.
112
112
00:05:37,570 --> 00:05:43,820
So we can just say currentSpeed
is now assigned as speed.
113
113
00:05:43,820 --> 00:05:48,110
So if we go on passing 10, which we're
doing here, we're passing in 10,
114
114
00:05:48,110 --> 00:05:51,050
then we'll be multiplying
by 10 in the currentSpeed.
115
115
00:05:51,050 --> 00:05:53,203
Okay, that looks pretty sharp, I reckon.
116
116
00:05:53,203 --> 00:05:56,070
Save that, back over into Unity.
117
117
00:05:56,070 --> 00:06:01,751
Now, let's see what it's going to do,
click on Play Okay,
118
118
00:06:01,751 --> 00:06:04,732
our guy should jump, walk,
walk, walk, and then [SOUND].
119
119
00:06:04,732 --> 00:06:08,360
Takes off super fast when it gets to this
point where it's going 10, that's awesome.
120
120
00:06:08,360 --> 00:06:11,090
Now, just a little bit of playing around
here, having a little bit of fun.
121
121
00:06:11,090 --> 00:06:16,060
You could, at this point of your timeline,
say, walk normally, and then stop.
122
122
00:06:16,060 --> 00:06:20,332
And that is as simple as saying,
add a new animation event,
123
123
00:06:20,332 --> 00:06:24,097
find our name,
which we know is SetMovementSpeed.
124
124
00:06:24,097 --> 00:06:27,791
If you have any issues with this,
the first thing to do is make sure you're
125
125
00:06:27,791 --> 00:06:31,024
spelling it the same way there
as you're spelling over in here.
126
126
00:06:31,024 --> 00:06:34,187
And like I said, it's good to copy and
paste to make sure it's all good.
127
127
00:06:34,187 --> 00:06:36,260
I'm gonna set this at 0, Save.
128
128
00:06:36,260 --> 00:06:37,840
As simple as that, click on Play.
129
129
00:06:39,220 --> 00:06:41,360
Walk, walk, jump in first.
130
130
00:06:41,360 --> 00:06:45,691
Jump, boink, walk, walk, walk,
stop, and then [SOUND] take off.
131
131
00:06:45,691 --> 00:06:48,460
And then we could add another one
down here that stops it or starts it.
132
132
00:06:48,460 --> 00:06:52,330
So this is a great way to get tons of
control, not just over how your frames
133
133
00:06:52,330 --> 00:06:55,990
are playing with the animation, but also
the other things that you want your thing,
134
134
00:06:55,990 --> 00:06:57,740
your character, to be doing on screen.
135
135
00:06:57,740 --> 00:07:01,570
We could also get in here and
manipulate our frame.
136
136
00:07:01,570 --> 00:07:05,490
So I could stretch this out, and here,
if I wanted to have the character walk
137
137
00:07:05,490 --> 00:07:09,820
slowly, I grab that, I drag it, then
we can have our frames represent that.
138
138
00:07:09,820 --> 00:07:12,680
But I would need to grab our animation
event and move that back as well.
139
139
00:07:12,680 --> 00:07:16,580
So you can do a lot of monkeying around,
you can play around in here.
140
140
00:07:16,580 --> 00:07:19,440
Let's go and
implement our sensible solution now.
141
141
00:07:19,440 --> 00:07:22,700
So I'm going to shift this all
the way back to the start,
142
142
00:07:22,700 --> 00:07:26,340
if I can grab it properly, all the way
back to the start, delete this one.
143
143
00:07:26,340 --> 00:07:29,410
One more time, Rick,
learn how to use a mouse, would you?
144
144
00:07:29,410 --> 00:07:31,390
Okay, we're gonna set this to 1.
145
145
00:07:31,390 --> 00:07:34,460
That's the speed we want him to
be walking when he is walking.
146
146
00:07:34,460 --> 00:07:37,761
Find the other animation, which was,
a lot of the reason we were doing this
147
147
00:07:37,761 --> 00:07:41,990
is cuz when the character was jumping,
it was moving at one speed and sliding.
148
148
00:07:41,990 --> 00:07:44,500
So we add an animation
event at the start here.
149
149
00:07:44,500 --> 00:07:49,280
We paste in our SetMovementSpeed,
and change our float to 0,
150
150
00:07:49,280 --> 00:07:51,130
which it is already by default.
151
151
00:07:51,130 --> 00:07:52,330
That is excellent.
152
152
00:07:52,330 --> 00:07:54,750
Save that up, now see if this works.
153
153
00:07:54,750 --> 00:07:58,510
We want our character to jump,
not move, and then start walking.
154
154
00:07:58,510 --> 00:08:02,431
So jump, stationary, and
then start walking, awesome.
155
155
00:08:02,431 --> 00:08:06,756
And that is because, in our Animator,
we see that our character,
156
156
00:08:06,756 --> 00:08:10,090
where are you,
Animator Controller, here we go.
157
157
00:08:10,090 --> 00:08:12,610
We see that our Jump is first to happen.
158
158
00:08:12,610 --> 00:08:15,770
It's going at 0 speed, and
then we transition into Walk.
159
159
00:08:15,770 --> 00:08:20,302
And the very first frame of Walk, we are
telling it, through an animation event,
160
160
00:08:20,302 --> 00:08:25,140
where is my walk animation event,
we're telling it to move at speed of 1.
161
161
00:08:25,140 --> 00:08:28,330
Okay, cool, so
really super useful technique.
162
162
00:08:28,330 --> 00:08:30,610
This is something you can
use in fighting games, for
163
163
00:08:30,610 --> 00:08:33,100
example, if you wanna have
a character reaction.
164
164
00:08:33,100 --> 00:08:36,340
So right in the middle of
the frame is where they,
165
165
00:08:36,340 --> 00:08:40,140
it looks like they're punching the other
character, or punching the enemy,
166
166
00:08:40,140 --> 00:08:42,660
then that is the frame that
you can trigger the damage.
167
167
00:08:42,660 --> 00:08:44,990
So if you've got this big long swing,
168
168
00:08:44,990 --> 00:08:48,910
you're not having the code trying to
guess where the actual impact took place.
169
169
00:08:48,910 --> 00:08:53,520
You can do it right down to the exact
frame of this is exactly where the bullet
170
170
00:08:53,520 --> 00:08:57,370
strikes, or the punch hits,
or the character jumps.
171
171
00:08:57,370 --> 00:09:00,070
And you can be triggering all
manner of exciting things,
172
172
00:09:00,070 --> 00:09:03,510
sound effects, changes in variables, etc.
173
173
00:09:03,510 --> 00:09:08,130
So there it is, there's the technique
of using the animation event to
174
174
00:09:08,130 --> 00:09:10,060
add more depth to our animations.
175
175
00:09:10,060 --> 00:09:11,540
Great work, and
I'll see you in the next video.
17736
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.