All language subtitles for 001 Making the First Enemy AI[UdemyIran.Com]

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

In this video, we're going to do something very exciting and create our first enemy AI.

We already have an enemy blueprint, right?

With the crab that we can attack, that can be defeated, that can deal damage to us, but it doesn't

really have a brain.

It's not really doing anything.

So through AI behavior, we can make it walk left and right.

For example, turn around if it hits the wall and turn around before it walks down a ledge.

And this is going to be a simple implementation for the crab that you've probably seen in many retro

games before.

There is a big problem in misconception with AI in Unreal Engine though, and nobody really knows how

to do it properly.

There's barely any good tutorials and everybody just thinks they are supposed to use behavior trees,

because this is the one AI feature that comes with Unreal Engine.

But behavior trees are very complicated, and actually you don't need something that complex for most

AI applications.

So the way that I use AI mostly and teach AI is to simply implement the AI in the AI controller through

blueprints.

So let's start by creating a base AI controller that is shared between all of the enemies, and then

make an AI controller for the crab.

So here in the content drawer, we go to blueprints and we can just right click here and make a new

folder called AI.

And in here we can right click again blueprint class.

And the one we want is not in the comment section.

We actually have to look for AI controller.

And here you see the AI controller above below the controller.

So we just want to use this one and click on select.

And I'm going to call it AIC base because again this is just for all of the enemies.

If we maybe want to have something to share in there.

And after that we just want to right click and create Child Blueprint class and called AIC crab for

the crab.

And now let's open this up.

In here again, you see the viewport, you see an event graph.

And this is also just a regular blueprint.

However, on the right side you can see that we already have a lot of things implemented here.

Here we have a lot of settings for the AI and things like that, but we don't really have to worry about

those.

We can just leave them at the default setting for now, and you could tweak them later on if you need

to.

But basically everything that we need for the AI to work is already here with the component and the

path following component, and all of the things that we have here, and we can just build on top of

that.

Again, just so you know, what we want to build for the crab.

The crab isn't an aggressive enemy.

It doesn't really care about the player.

It doesn't have to detect the player.

It just kind of minds its own business and it's always just going to walk forward.

So here if we start, it's going to start walking forward.

And then if it hits the wall, we want it to detect that it hits the wall and then turn around and then

just walk forward.

And then if it detects, oh, I'm going to fall down a ledge, we're going to catch it and turn it around

again.

So it's just going to go back and forth back and forth.

And I'm pretty sure you've seen enemies like this thousands of times in many platformer games.

And that is the first one I want to teach you.

And you could also implement all of that simply in the crabs blueprint.

That would also work, but it's a better separation of concerns pattern to use an AI controller here.

And we can treat the AI controller kind of like the brain of the crab in this case, and it's going

to give instructions to the crab of what it should execute and what it should do.

Here we also have the on Beginplay event and the on tick event.

But actually with an AI controller you have to be careful because the Beginplay event is actually a

little bit dangerous here.

There is a better event we can use which is called on possess.

Event on possess.

And this is triggered not when the AI controller spawned, not when the game is started, but if this

AI controller possesses a pawn.

And this is also going to tell us which pawn we are possessing.

The problem that happens on Beginplay is that sometimes the AI controller will spawn before the actual

pawn spawns, and if we then try to get a pawn, for example, get controlled pawn, right?

So if we try to get the controlled pawn on beginplay, sometimes it is not going to have a reference,

it's just going to be null because they were spawned not in the correct order.

So we got to be careful with Beginplay.

Let's actually just delete it so we don't make any mistakes.

It is better to just use the event on possess, and we also get the pawn right here and can save it,

can use it however we want.

And how do we want to use this?

Well first we want to cast to be enemy grab.

Like this and save this.

So promote the variable and just say owning grab.

Like this.

And now we can use this in other parts on tik and other parts of this AI.

Controller.

Blueprint.

And just to show how this works, we want to print string, print string and just print the owning crab.

And right now if we start, well, nothing is going to print because we still have to tell the crab

to use this AI controller.

So I can go to the content drawer, go to blueprints, characters enemies, and have the enemy crab

and open up the full blueprint.

And here, while this is selected, just look for AI.

And scroll down.

And here you can see the AI controller class.

And instead of the general AI controller we just select this and use the AIC grab.

And if we just start the game now you can see here we can see it prints the BP enemy crap at the top

left.

So this proves to us that this is actually working.

This is getting the reference of the crap and it's saving it.

So we can then use it here.

One important setting here though, is also on the enemy crap under eye you can see auto possess eye,

and this is a setting that trips up so many of my students.

I've seen it hundreds of times and honestly, I don't understand why this is the default setting placed

in world.

This means that the eye controller is only gonna possess this pawn if it is placed in the world.

However, if you spawn it through a spawn system or something like that, it is actually not going to

be possessed by the eye controller and it's just going to be floating in the air.

And we want to set this to spawn a placed in world or spawned.

So it's gonna control the pawn in both cases.

But actually we don't want to set this in a crap.

We want to set this in the enemy base, because this is a setting we want to have for all of them.

So VP enemy base.

And here just look for eye again.

And all the way at the bottom we just want to set this to placed in world or spawned compile and save.

And now you can see in the enemy crap.

It is also changed because we changed it in the parent.

And now this is going to be applied to all of the enemies we're going to create later on.

And now we are ready to give the crab instructions.

And what do we want the crab to do?

Well, just tell it to walk forward constantly.

But first we have to create a function in the crab to walk forward because we haven't actually implemented

anything in the crab.

So here we can just make a custom event.

Custom event and call it Walk Forward.

Like this.

And here, just like for the player, we want to trigger the add movement.

Input.

Add movement input.

And the world direction in this case, because we always want to walk forward, is not just typing X

on the one, but here we actually just want to get actor forward vector.

So we always walk in the direction our actors pointing towards.

And for the scale value.

Well, we had a controller here for the player right.

So for the player it could be minus one for left and plus one for right.

But now the AI controller is going to give us this value.

So we can just go here and place it here.

Like this and just expose it, make it an input and I can compile and save.

And now in the AIC grab on tick.

Because on every frame we want to slowly walk forward, I can just get well, first of all get rid of

the print string here and here on tick.

Because on every frame we want to walk forward, just get the grab.

And here just walk forward, which is the custom event we created here.

Right.

Just call this.

Go like this and always just have the scale value of one, which is a full walk.

And this is the equivalent of just tilting your stick all the way to the right.

And let's check this out.

Now I can compile, save and go here.

And the crab is walking forward.

Right.

It might be a little bit hard to see.

So we can just go to simulate instead.

Right.

And you can see the crab is walking forward.

But it is very very fast.

Let's just put in another crap so we can better test how fast it is.

Because here we're just going to hit the wall right away.

So we can just select the crab, hold alt on the keyboard, and drag off from this red arrow to make

a copy.

And again, we can see that it is very fast.

So in the BP enemy crab we can just click on the character movement component.

And here we look for max walk speed.

And it is 600 right now, which is exactly as fast as our player.

But crabs are supposed to be very slow, so just put it to 120 and we can compile, save and check it

out now.

And yeah, I think this is a good value for our simplest and easiest enemy.

And yeah, this is working fine, but you can see that we are only playing the idle animation.

And just like we did for the player, we want to check that if we are moving, we want to play the walking

animation instead.

And to set up the animation.

We actually have two choices now and we can go through the content drawer.

Go to content, paper, assets, characters, enemies, and crab.

And you can see that we have way fewer animations here than we have for our player, right?

And for something as complicated as the player, of course, we want to use paper because it gives us

much more control.

It makes calling events easier.

It gives us this nice animation graph where we can also use jumps.

But for this simple character, we just have an idle animation and a walk animation.

So it is actually very easy for us to just control this in the regular blueprint without relying on

the Paper XRD plugin, just using the basic paper 2D functionality.

And here there is no really wrong or right way, right?

Because if we go to blueprints, characters and player right, you can see we had to create this animation

source, we had to create this animation blueprint, and then we had to set it up in here.

So we had to go through a bunch of different steps to set up the Paper XRD for this character.

And since the setup takes some time, it makes it more worth it the more we're going to implement.

But for the crab, I believe just implementing this in the blueprint is actually quicker and simpler

than using Paper XRD.

And I actually want to show you that this is also an option.

But in the end, you kind of can decide for yourself if you just want to have everything use the same

method, that is fine too.

I just want to show you that both of these options are available.

So now in the BP enemy crab, we want to go to the tick function here.

And we just want to create a new function and call it update Anim.

It's called update anim.

And we want to now call this on tick right.

So after the parent tick just call the update anim.

And in here we're just going to implement our animation switching now.

And how do we do this without paper.

Well we're not going to use the animation component.

We're not going to use an animation blueprint.

We can just get the sprite.

And actually this is a flipbook component.

So the name sprite is a little bit unfortunate.

But we can't change it because it's inherited.

But this is actually a flipbook right.

This flipbook that is playing here and we can directly affect it.

So back here right.

We want to set flipbook.

Which is a built in function here, and just drag it off like this.

And here we can then select which flipbook we want to place.

So just type in grab and we can then select do we want to play the idle or do we want to play the walk.

And we decide this based on our velocity so we can get velocity.

Like this.

And here just the vector length.

Because we don't care about the direction.

And we just want to check if this is bigger than zero, which means we are moving.

And we can then use this with a select here.

So I can just select.

You could also use a branch and then put this in here.

Right.

So then I just get a true and false flipbook.

So if this is true then we know that we are walking.

We are in motion.

So here I can just select the crab walk.

And for false we are idle.

So I can just select the crab idle.

And you could also use a branch here.

Right.

But I think the select is just makes it easier to look at and makes it simpler.

And this is all it takes right.

So this was way quicker than setting up the animation source, setting up the animation blueprint,

and doing all of those things so I can just compile and save.

Now go here simulate and you can see we are playing the walk animation now properly.

And now the next thing we want to do is to make the crab spin around once it hits the wall.

And for this there are also multiple methods.

We could use a line trace again like we did for the player to detect the wall.

But I want to show you the other alternative options, and there's not really one option that is better

than the other.

It kind of depends on your preference and the case.

But here I'm going to use a box collision.

So in the enemy crab go to the viewport.

And while the capsule component is selected just click on add and look for box Collision like this.

And we then want to put this to the front side.

Right.

You can see this arrow here.

If I click away this uh bluish light blue arrow this is the forward direction.

And we want to have the box in front of the player the crab.

But first we also want to rename it to Wall Detector.

So here we want to move it over to the right side.

And here make it a little bit shorter.

Smaller on this side.

The with back and forth is fine.

It should be around the same as the capsule.

And then we want to move it over here where the claw ends.

So we're gonna start turning around once we hit this area.

As always, we now have to set up the collision for the wall detector, so select it on the right side.

And again, we don't have to make a preset for this.

This is very simple.

So just go to custom, ignore everything and overlap only world static because again floors.

Walls are usually world static.

And now go to the UN compound begin overlap event, add it.

And for now other actor.

We just want to print string here.

Print string the other actor like this and let's check it out.

There's actually going to be one problem, but let's have a look.

It doesn't detect the wall.

And this is actually a problem with the settings on the cube grid tool output.

If we go in here and look for collision.

You can see that we are using the default settings for the collision presets.

So this part is fine, but we actually cannot generate overlap events.

So if you open this up a little bit more you can see that generate overlap events is not checked.

So make sure on the cube grid tool output to check generate overlap events.

And you got to be careful.

There's actually two that are similar.

Right there is generate overlap events during level.

Uh, level streaming.

We only care about the first one.

If you make a level with level streaming, then yeah, you might also want to check this.

So we can just check both just to make sure.

And then get out of here and let's try it again.

And now you can see we are actually printing this reference to the cube grid tool.

So this is working fine.

Now we just have to actually turn the crap around.

If this happens to do that, we just need to create a function that turns the crab around.

But this seems like a very general thing that we might also use in other enemies.

So I think it's better to just put this in the BP enemy base.

So make sure to open up the BP enemy base and create a new function and just call turn character.

And here we just want to set actor rotation.

Like this.

And what do we want to do?

Well, we want to get the current rotation.

So get actor rotation.

Because we need to know if we are looking left or right right now, and we can actually just add 180

degrees to it so we can combine rotators.

Here and on the Z.

Right?

Because if you look here the Z axis sorry.

Got to go here on rotation.

Right.

The z axis is the one that turns us around in this way.

Right, but set it back to zero.

And this was just to show you.

And yeah.

So back in the function we just want to put 180 for the Z and then connect this here.

And this is all it takes to turn the character around here in the function.

And now we just want to call this in the BP enemy crab.

So instead of the print string here we can just call turn turn character like this.

Now let's check it out.

Can go back to the map.

We hit the wall.

We turn the character.

It's working fine, but now we're actually falling down the ledge.

And again, this depends on what kind of game you're making.

Maybe you want to have this kind of behavior, but in this case, I also want to show you how we can

detect the ledge and then turn around at the ledge.

There's actually one more cool thing we can easily add.

And if we just copy the crab again, hold alt and pull here.

And you can see that now they just walk through each other.

But we can actually easily set it up.

So they also start turning around if they hit each other.

So on the enemy grab for the wall detector.

If we set world dynamic to overlap as well, this is actually going to work.

And now if we do this.

They kind of hit each other and turn around as well.

And this is, I think, a really cool effect that you also see in a bunch of these games.

With this though, we have to be a little bit careful and for now world dynamic is fine.

But as you add more things, there's maybe going to be other things in world dynamic that you don't

want to affect this.

So later on maybe we want to add another preset, but for now this is fine as long as we don't have

any bugs.

And I think it's just a little nice cool effect that we can add.

So let's now get into the ledge detection.

And this works in a similar manner.

Again, while the capsule component is selected click on Add and look for box Collision again like this.

And this one we want to call Ledge Detector.

And this one.

We also want to drag it over to the side and make it a somewhat similar size to the other one.

Like this.

But this one is actually important.

We want to move it downward because this is always going to check if there is a floor overlapping with

us or not.

Right.

So it's always going to check.

And this actually we can go a little bit more left like this.

And yeah we always want to make sure that a floor is in this collision.

And the moment we don't detect any floor in here, we're actually going to start turning around.

And here again on the right side we got to set up the collision settings.

And here again we can just go to custom ignore everything and only check for world static.

And here we actually want to use the opposite event not begin overlap but actually end overlap.

Because we want to turn around the moment.

There is no floor beneath us anymore.

Just add this.

And here we can also just copy paste this turn character like this and put it here and compile and save.

And let's check this out.

Right.

We hit the ledge and we turn around.

And this is how easy it is to implement this system.

Right.

And everything we've done here in the AI controller is literally just a walk forward on the event tick,

and everything else happens in the BPM.

You could have also done this on tick in the BPM, right?

But it is a better separation of concerns pattern to kind of treat the AI controller like the brain.

So the brain is going to tell it walk forward, walk forward, walk forward and the body is the enemy

craft blueprint.

It's going to execute what it's being told.

And this pattern becomes more useful the more complicated your enemies get.

Next up, we're going to make an enemy that can shoot projectiles.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.