Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,080 --> 00:00:04,820
In this video, we want to set up the BP enemy, which will be our last basic enemy.
2
00:00:04,820 --> 00:00:09,140
It's going to have an aggro detection system, so if the player comes closer, it's going to start targeting
3
00:00:09,140 --> 00:00:11,000
the player and also chase it.
4
00:00:11,590 --> 00:00:16,840
And again, this is based on the BPM base which is based on the BP action chart base.
5
00:00:17,380 --> 00:00:19,990
As always, let's start out by making the blueprint.
6
00:00:19,990 --> 00:00:24,730
So on the content draw blueprints, characters, enemies.
7
00:00:24,730 --> 00:00:33,250
Right click the BP enemy base and create Child Blueprint class and call this BP enemy AI and open it
8
00:00:33,250 --> 00:00:33,730
up.
9
00:00:36,440 --> 00:00:37,460
Click the sprite.
10
00:00:37,460 --> 00:00:41,900
And here we just want to look for the I the fly I idle.
11
00:00:41,900 --> 00:00:44,060
And again this only has an idle animation.
12
00:00:44,060 --> 00:00:45,500
It doesn't have any other animations.
13
00:00:45,500 --> 00:00:51,260
We don't have to worry about animation switching, and we can just make the capsule a little bit smaller
14
00:00:51,260 --> 00:00:57,320
to better fit the shape, and also just move the sprite around a little bit here to center it.
15
00:00:58,290 --> 00:00:59,850
And yes, this is good enough.
16
00:01:01,300 --> 00:01:04,060
Now let's create the AI controller for this enemy.
17
00:01:04,060 --> 00:01:07,660
So in the content draw blueprints AI.
18
00:01:07,660 --> 00:01:14,800
And again based on the AIC base, create a Child Blueprint class and just called AIC underscore AI.
19
00:01:15,630 --> 00:01:16,950
And open this up.
20
00:01:17,560 --> 00:01:21,010
And on the event graph, we just want to do the same thing again.
21
00:01:22,200 --> 00:01:23,640
We don't use beginplay.
22
00:01:23,670 --> 00:01:27,180
We want to use the on possess just because it is safer.
23
00:01:27,180 --> 00:01:29,460
We know there aren't going to be any timing issues.
24
00:01:30,370 --> 00:01:35,650
And here we can just drag off the possessed pawn and cast to be enemy.
25
00:01:35,680 --> 00:01:36,430
I.
26
00:01:37,740 --> 00:01:42,000
And then we just save the reference and it's called owning I.
27
00:01:45,000 --> 00:01:46,440
And we can move this up here.
28
00:01:47,530 --> 00:01:51,040
And how do we get a reference to the player that we want to target?
29
00:01:51,070 --> 00:01:53,980
Well, first let's just ignore the aggro system.
30
00:01:53,980 --> 00:01:59,620
We're just going to forcefully get a reference to the player whenever the game starts or when we use
31
00:01:59,620 --> 00:02:01,060
the on possess event.
32
00:02:01,360 --> 00:02:08,949
And we can do this simply by right clicking and get player pawn, which is a shortcut that is an Unreal
33
00:02:08,949 --> 00:02:09,190
engine.
34
00:02:09,190 --> 00:02:14,560
And we can just get a reference to the player pawn and index zero is the player one, and we only have
35
00:02:14,560 --> 00:02:16,600
one player, so we don't have to worry about the index.
36
00:02:16,600 --> 00:02:19,090
And this we just want to save it in a variable.
37
00:02:19,090 --> 00:02:23,080
So we can just promote the variable and just call it target pawn.
38
00:02:24,060 --> 00:02:25,080
Like this.
39
00:02:26,180 --> 00:02:30,140
And for now, let's just print string this to see that it works.
40
00:02:30,680 --> 00:02:33,500
Print string and connect the target pawn.
41
00:02:33,590 --> 00:02:34,160
Right.
42
00:02:34,160 --> 00:02:37,820
And we have to use this AC enemy in the BP enemy.
43
00:02:38,400 --> 00:02:41,700
Click the top level and type in I.
44
00:02:41,760 --> 00:02:48,570
And as always for the AI controller class, just select the AIC and compile save.
45
00:02:48,570 --> 00:02:51,300
And let's just check if this works well.
46
00:02:51,300 --> 00:02:57,480
We first have to put it in the stage, not the AI controller, but the blueprint characters and the
47
00:02:57,480 --> 00:03:02,460
enemies BP, enemy AI and just put it in the stage somewhere.
48
00:03:02,550 --> 00:03:04,380
You can just drag it up here.
49
00:03:05,800 --> 00:03:06,700
Like this.
50
00:03:06,700 --> 00:03:12,460
And if we click on simulate, we actually have an issue because the player doesn't exist, so it doesn't
51
00:03:12,460 --> 00:03:13,870
know what it's supposed to target.
52
00:03:13,870 --> 00:03:16,660
But also you see that it just falls down.
53
00:03:16,660 --> 00:03:19,120
So let's go to the enemy.
54
00:03:19,120 --> 00:03:25,450
And also here for the character movement, we just want to look for a movement mode.
55
00:03:26,130 --> 00:03:30,120
And select for default land movement mode, just select flying.
56
00:03:30,760 --> 00:03:32,440
And again, I can simulate now.
57
00:03:32,440 --> 00:03:33,160
And it's flying.
58
00:03:33,160 --> 00:03:34,030
It's all fine.
59
00:03:35,110 --> 00:03:40,390
And even if we start the game now with selected viewport, we don't see the message being printed.
60
00:03:40,390 --> 00:03:44,980
And this is a timing issue that can happen in the IE controller.
61
00:03:44,980 --> 00:03:50,500
And if we just use delay here, this delay 0.2 and this is just for testing.
62
00:03:50,500 --> 00:03:52,630
But now this actually solves the problem.
63
00:03:52,630 --> 00:03:55,390
And you can see we get the beep player zero.
64
00:03:55,390 --> 00:04:01,420
And this is again with the timing because in this case event on process might happen before the player
65
00:04:01,420 --> 00:04:02,560
is spawned in the map.
66
00:04:02,560 --> 00:04:05,230
And we can just prevent this with the delay.
67
00:04:05,230 --> 00:04:06,820
But in the end we're not going to use this.
68
00:04:06,820 --> 00:04:09,160
This is a kind of dirty quick solution.
69
00:04:09,160 --> 00:04:12,310
But for now, just to help us implement this, it is fine.
70
00:04:12,310 --> 00:04:14,800
And we'll replace this with the aggro system later.
71
00:04:15,500 --> 00:04:21,170
And now that we have a reference to our self and a reference to the target pawn on tick, we always
72
00:04:21,170 --> 00:04:27,800
want to know which direction from us the player is located in and just fly towards that direction and
73
00:04:27,800 --> 00:04:30,530
we can again use the look at rotation.
74
00:04:30,650 --> 00:04:38,990
So find look at rotation and check from the eye, not from this eye controller, but check from the
75
00:04:38,990 --> 00:04:44,240
owning eye and get actor location as the starting point.
76
00:04:44,240 --> 00:04:47,030
And we want to look at the target.
77
00:04:47,030 --> 00:04:48,440
So the target pawn.
78
00:04:49,110 --> 00:04:51,780
And here we get actor location.
79
00:04:52,330 --> 00:04:54,820
Like this and connect this here.
80
00:04:54,820 --> 00:04:56,920
And again, we want to know the direction.
81
00:04:56,920 --> 00:04:58,180
We don't care about the rotation.
82
00:04:58,180 --> 00:05:01,810
We just want to get forward get forward vector.
83
00:05:01,810 --> 00:05:06,220
And now we simply always have to move forward in this direction.
84
00:05:06,220 --> 00:05:08,440
And it's always going to follow the player.
85
00:05:08,830 --> 00:05:11,650
And just to demonstrate we can use the tick.
86
00:05:12,330 --> 00:05:14,160
And here just print string.
87
00:05:14,850 --> 00:05:16,950
And print the forward vector.
88
00:05:16,950 --> 00:05:23,010
But actually we want to make sure that we have a target because if we despawn the player, it could
89
00:05:23,010 --> 00:05:24,660
happen that we don't have a target.
90
00:05:24,660 --> 00:05:28,500
And then we're going to get issues with the tick event here.
91
00:05:29,090 --> 00:05:34,520
So here we first want to make sure that owning AI is valid.
92
00:05:36,060 --> 00:05:36,840
Like this.
93
00:05:39,040 --> 00:05:43,150
And we also want to make sure that the target pawn.
94
00:05:44,010 --> 00:05:45,000
Is valid.
95
00:05:45,650 --> 00:05:50,090
Because otherwise we're going to get an error message on tick if we don't have a target.
96
00:05:50,840 --> 00:05:51,650
Like this.
97
00:05:52,290 --> 00:05:53,820
And now let's just try it out.
98
00:05:53,820 --> 00:05:54,750
Compile and save.
99
00:05:55,260 --> 00:05:59,610
And you can see that if I move it's going to change the vector.
100
00:05:59,610 --> 00:06:03,150
So this tells us that the AI is correctly tracking the player.
101
00:06:03,150 --> 00:06:09,780
But now to actually implement this movement, we have to go to the enemy AI and implement a movement
102
00:06:09,780 --> 00:06:12,090
function like we also did for the crab.
103
00:06:12,630 --> 00:06:17,070
So in the event graph we just want to create a custom event.
104
00:06:17,640 --> 00:06:20,550
And call it move towards.
105
00:06:23,180 --> 00:06:27,470
And here we just use the add movement input again.
106
00:06:28,740 --> 00:06:30,930
And this time we don't want to go forward.
107
00:06:30,930 --> 00:06:37,740
We don't want to just go a set value on the X, but we actually want to get this value from the AIC,
108
00:06:37,770 --> 00:06:42,810
right, to get the forward vector towards the enemy or towards the player, in this case the target.
109
00:06:43,380 --> 00:06:45,360
So here we can just put this in here.
110
00:06:45,360 --> 00:06:47,910
And it's going to create an input and the scale value.
111
00:06:47,940 --> 00:06:49,620
We can also just connect it here.
112
00:06:49,620 --> 00:06:52,650
And again one is going to make us go at full speed.
113
00:06:52,650 --> 00:06:55,110
0.5 is going to make us go at half speed.
114
00:06:55,110 --> 00:07:00,180
And I just want to set the default value for this so we can compile and save.
115
00:07:00,180 --> 00:07:05,640
And for the scale value I just want to set the default value to 1.0, because this just makes sure if
116
00:07:05,640 --> 00:07:08,490
you forget to fill it out, it's going to use 1.0.
117
00:07:08,880 --> 00:07:12,810
So back in the AIC we can now get rid of this print string.
118
00:07:12,810 --> 00:07:21,540
And here if it's valid we just want to actually get the owning I copy this paste this and then just
119
00:07:21,540 --> 00:07:26,970
move towards the function which we just created and go here.
120
00:07:26,970 --> 00:07:30,390
And then we just use this forward value and scale value.
121
00:07:30,420 --> 00:07:31,680
We can just leave it at one.
122
00:07:31,680 --> 00:07:36,930
You could implement a system where maybe it flies faster or slower at different times, different intervals.
123
00:07:36,930 --> 00:07:41,340
But now yeah, just leave it at one and we can compile and save and check this out.
124
00:07:41,730 --> 00:07:43,350
And you can see it is chasing me.
125
00:07:43,350 --> 00:07:44,700
It is actually very scary.
126
00:07:44,700 --> 00:07:46,110
It is very fast.
127
00:07:46,110 --> 00:07:48,180
But I actually like this.
128
00:07:48,210 --> 00:07:51,930
It's it's supposed to be a strong and imposing enemy.
129
00:07:51,930 --> 00:07:57,480
And you can see even here it's going to try to get us, but it has a little bit too much health as you
130
00:07:57,480 --> 00:07:58,620
can see as well.
131
00:07:58,740 --> 00:08:07,140
So in the enemy here we just want to set the health actually on BBC vitality set the health to five.
132
00:08:07,140 --> 00:08:11,100
In this case it's supposed to be a bit stronger but ten is a little bit too strong.
133
00:08:11,830 --> 00:08:13,990
And we can also adjust the speed a little bit.
134
00:08:13,990 --> 00:08:18,100
So I can tell you how this works, because on the character movement component, this time around,
135
00:08:18,100 --> 00:08:20,650
we actually don't want to use max walk speed.
136
00:08:20,680 --> 00:08:23,800
We actually want to use the fly speed.
137
00:08:24,680 --> 00:08:26,600
So here you see the max fly speed.
138
00:08:26,600 --> 00:08:28,700
And let's just set this to 400.
139
00:08:28,700 --> 00:08:31,580
And this is how you can apply the flying speed.
140
00:08:31,580 --> 00:08:32,780
Let's check this out.
141
00:08:33,429 --> 00:08:36,220
And the 400 is still 600 is actually.
142
00:08:36,220 --> 00:08:40,179
Yeah, a little bit too crazy because it would be exactly as fast as our player.
143
00:08:40,179 --> 00:08:42,610
So 400 gives us a little bit more leeway.
144
00:08:42,610 --> 00:08:43,750
It is still imposing.
145
00:08:43,750 --> 00:08:44,920
It is still strong.
146
00:08:45,040 --> 00:08:50,590
Before we get into the aggro system I just want to change the direction because now the AI is always
147
00:08:50,590 --> 00:08:53,470
looking to the right, even if the player's on the left of it.
148
00:08:53,470 --> 00:08:57,760
So we just want to implement this quickly in the BP enemy.
149
00:08:57,790 --> 00:09:04,030
We can now go to the move towards event and after the add movement input, we just want to set actor
150
00:09:04,030 --> 00:09:05,140
rotation.
151
00:09:05,620 --> 00:09:11,800
And here again we can use our good old friend select Utility Select and we can then decide if we pick
152
00:09:11,800 --> 00:09:15,100
option one or option zero depending on an input.
153
00:09:15,100 --> 00:09:19,630
And while everything we have to check here is from the world direction we can break.
154
00:09:20,230 --> 00:09:26,200
And we only need the X because again, if the x is negative, because again, if the x is negative,
155
00:09:26,200 --> 00:09:30,400
the AI is going to want to move to the left side because the players on the left side.
156
00:09:30,400 --> 00:09:33,490
And if the X is positive, it's going to want to move to the right side.
157
00:09:33,490 --> 00:09:37,660
And we can simply use this again with a smaller check.
158
00:09:39,000 --> 00:09:42,750
Just check for less and use the result here as the index.
159
00:09:46,830 --> 00:09:53,370
And if this is true, this means that we want to go left so we can just set the actor rotation to 180.
160
00:09:54,530 --> 00:09:58,490
And if this is false, we just want to look right so we don't have to change anything.
161
00:09:58,490 --> 00:09:59,600
Just leave it at the zero.
162
00:09:59,600 --> 00:10:01,700
And this is actually how easy it is.
163
00:10:03,010 --> 00:10:05,020
Just try it out and look at this.
164
00:10:05,020 --> 00:10:06,130
I want the left side.
165
00:10:06,130 --> 00:10:07,240
It's looking left.
166
00:10:07,240 --> 00:10:07,960
I'm on the right side.
167
00:10:07,960 --> 00:10:09,010
It's looking right.
168
00:10:09,010 --> 00:10:12,280
And I'm actually getting hammered by all of these enemies now.
169
00:10:12,280 --> 00:10:13,840
But this is how it works.
170
00:10:14,080 --> 00:10:17,950
As the last thing for this enemy, we want to actually set up the aggro system.
171
00:10:17,950 --> 00:10:26,050
So in the AC instead of on beginplay with the delay, just getting the target pawn and setting it here.
172
00:10:26,050 --> 00:10:31,300
We just want to get rid of this, and we just want to create a small custom event here.
173
00:10:31,300 --> 00:10:36,730
So just right click Custom event and just set and just set target player.
174
00:10:38,520 --> 00:10:41,280
And here we just want to get the target pawn.
175
00:10:41,430 --> 00:10:42,360
Set it.
176
00:10:43,050 --> 00:10:44,760
Here and just connect this.
177
00:10:44,940 --> 00:10:48,000
And this is just a very simple function we can use.
178
00:10:48,880 --> 00:10:50,380
In the BP nmai.
179
00:10:50,410 --> 00:10:56,080
We now want to go to the viewport and while the capsule component is selected, add another thing.
180
00:10:56,080 --> 00:10:58,780
So here we want to look for collision again.
181
00:10:58,780 --> 00:11:02,920
And in this case a sphere collision is actually the best right.
182
00:11:02,920 --> 00:11:04,270
You cannot really see it now.
183
00:11:04,270 --> 00:11:08,170
But we just want to call it aggro range.
184
00:11:08,380 --> 00:11:10,900
And then here on the right side click away here.
185
00:11:10,900 --> 00:11:13,840
And we just want to make the sphere radius bigger.
186
00:11:16,300 --> 00:11:19,930
And it might be a little bit hard to judge here how big it should be.
187
00:11:19,930 --> 00:11:23,440
So we can just kind of look back at the map like this.
188
00:11:24,050 --> 00:11:25,940
And I think it should be a little bit bigger.
189
00:11:25,940 --> 00:11:27,560
So around 500.
190
00:11:29,140 --> 00:11:32,980
And here it looks really big, but in the map you can see it is not that huge.
191
00:11:32,980 --> 00:11:38,740
And if the player steps into this range, we want to make the AI aggressive and start attacking the
192
00:11:38,740 --> 00:11:39,340
player.
193
00:11:39,910 --> 00:11:42,160
Of course we have to set up the collision again.
194
00:11:42,160 --> 00:11:44,650
So on the aggro range to the right side.
195
00:11:45,540 --> 00:11:48,780
Scroll down and just make it custom.
196
00:11:48,900 --> 00:11:52,380
Ignore everything and just look for the pawn.
197
00:11:53,420 --> 00:11:58,490
Then scroll down to the on component Begin overlap event like this.
198
00:11:58,490 --> 00:12:02,840
And as we've done many times other actor cast to be player.
199
00:12:04,330 --> 00:12:05,110
Like this.
200
00:12:05,790 --> 00:12:09,060
And now we just want to set the target player.
201
00:12:09,720 --> 00:12:15,960
But the set target player function is actually not available here because it is on the eye controller.
202
00:12:15,960 --> 00:12:19,110
We first have to get a reference to the eye controller.
203
00:12:19,320 --> 00:12:22,380
So here we can just get eye controller.
204
00:12:22,740 --> 00:12:24,840
And this is again a built in function.
205
00:12:24,840 --> 00:12:27,570
And for the controlled actor we just pass self.
206
00:12:28,790 --> 00:12:29,630
Like this.
207
00:12:31,680 --> 00:12:39,240
But even this doesn't let us call the set target player because we need to cast it to the AIC.
208
00:12:39,270 --> 00:12:41,700
So here we can cast to AIC.
209
00:12:42,240 --> 00:12:42,900
I.
210
00:12:43,980 --> 00:12:45,060
And connect here.
211
00:12:46,280 --> 00:12:50,360
And now we can call set target player.
212
00:12:51,120 --> 00:12:52,350
Just like this.
213
00:12:52,350 --> 00:12:59,670
And then the target pawn is whoever is the overlapping BP player can just plug it in here.
214
00:12:59,670 --> 00:13:02,340
Double click to reroute it.
215
00:13:03,740 --> 00:13:05,510
And just like this.
216
00:13:10,270 --> 00:13:10,510
Mhm.
217
00:13:11,650 --> 00:13:18,610
So now, if you want to cast to the AI controller here or on Beginplay and save it, it kind of depends
218
00:13:18,610 --> 00:13:20,770
on if you want to use it multiple times.
219
00:13:20,770 --> 00:13:24,640
So it's better practice to just do it once here and save it in a variable.
220
00:13:24,640 --> 00:13:30,760
But actually the aggro range is only going to be active a single time because after we go through all
221
00:13:30,760 --> 00:13:35,470
of this, after we set the target player, we actually don't want to use this collision anymore.
222
00:13:35,470 --> 00:13:36,790
We can just turn it off.
223
00:13:36,790 --> 00:13:39,850
And this is never going to fire a second time.
224
00:13:40,590 --> 00:13:45,090
But for now, let's just check this out, see if it works and it's too close.
225
00:13:45,090 --> 00:13:48,150
Now, I can't really test this, so just move it over to the side.
226
00:13:49,100 --> 00:13:49,520
Right.
227
00:13:49,520 --> 00:13:50,630
So I'm standing here.
228
00:13:51,170 --> 00:13:52,160
It's not coming from me.
229
00:13:52,160 --> 00:13:53,300
It's not attacking me.
230
00:13:53,300 --> 00:13:59,600
I walk to the right and I enter the aggro range, and it's going to come from me and going to start
231
00:13:59,600 --> 00:14:00,170
chasing me.
232
00:14:00,170 --> 00:14:01,970
So this is working fine.
233
00:14:01,970 --> 00:14:04,160
Now, this is just a small thing.
234
00:14:04,160 --> 00:14:08,810
And like I said before, we just want to make sure that this can only be triggered a single time.
235
00:14:09,290 --> 00:14:18,980
So after the cast to BP player, I can just open up some space here and simply add a do once node the
236
00:14:18,980 --> 00:14:22,190
top one and never reset it, right?
237
00:14:22,190 --> 00:14:26,360
So this way we make sure that this is never active again.
238
00:14:27,490 --> 00:14:30,340
And now we're done creating four different unique enemy types.
239
00:14:30,340 --> 00:14:35,380
And I hope this taught you a lot about AI and how it isn't that complicated, and how you don't actually
240
00:14:35,380 --> 00:14:37,000
need to use behavior trees.
241
00:14:37,030 --> 00:14:41,170
In the next section, we're going to start looking into the user interface to show our health.
22064
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.