Free YouTube Transcribe

Video transcript

Smart Enemy AI | (Part 17: New Magic Spell) | Tutorial in Unreal Engine 5 (UE5)

Ali Elzoheiry · 4,475 words · 21 min read

Want to search this transcript, jump the video from any line, or download it as TXT, SRT, or VTT?

Open in the transcript tool

Full transcript

Outcome of This Part

0:00[Music]

0:05welcome back everyone to part 17 of our

0:08smart enemy AI series and this part I'm

0:11going to be showing you how to create

0:12this really cool mage uh

0:16attack so right now we have only one

0:18Mage attack or we had only one Mage

0:20attack before this tutorial and uh now

0:22I'm going to show you this what I call a

0:24barrage attack uh that fires multiple

0:27projectiles at the player in a random

0:29pattern so not directly at the player uh

0:32but around the player uh randomly so

0:35that it's harder to predict where

0:36they're going to

0:37go and uh of course we're going to be um

0:41integrating this in our Behavior tree to

0:43choose one of the two attacks to do uh

0:46based on a cool down so let's get

0:49started all right so um our Mage enemy

0:52so far has one attack so it teleports

0:55does this one attack and then teleports

0:58again and it's the same uh spell that

1:00the uh player

1:02has um so now I want to add a second

1:04spell for uh this Mage enemy so let me

Animation/Montage Setup

1:07start by showing you the animations so

1:10going to my characters Mage

1:14animations um Yep this is the animation

1:18I'm going to be using and again an

1:19animation I got from mixo I'll leave the

1:21name and a link down in the description

1:23uh but yeah it's basically this uh cast

1:26uh but we'll be modifying the animation

1:28a bit so I'm going to increase the

1:30windup time so that this takes a bit

1:32longer and then boom this happens all of

1:35a sudden and the part in the end I'm

1:37also going to increase his duration for

1:39a

1:39bit and what's going to happen during

1:42this animation is that the Mage enemy

1:43will continuously keep firing uh

1:46projectiles um at the

1:48player um all right so let's start by

1:50creating the Montage uh we go to our

1:52animation

1:54rightclick uh create an montage Call It

1:58montage

2:00uh

2:01Mage um barrage attack so I'm calling it

2:04barrage uh which uh is uh a term used in

2:09artillery Warfare to mean a concentrated

2:12attack on a specific area uh and I also

2:15see it in a bunch of other games I think

2:16I remember it from Runescape or

2:18something uh but I like the name um and

2:21I'm going to call it that um all right

2:24so because we're going to have um a

2:27windup time uh slow motion time and then

2:31the rest of the animation then I'm going

2:32to use it three times just like we did

2:35in the previous one as well uh so I'm

2:38going to take it once twice and three

2:41times so the first one I want it to end

2:45somewhere

2:47around yeah here

2:50maybe

2:550.65 and the second one will start at

2:580.65

3:00and end at

3:04around let's say

3:090.8 um and this first one I wanted to

3:12play at uh not its normal speed but like

3:16at 0.7 so a bit slower than its normal

3:18speed and the second one I want it to be

3:20a lot slower than its normal speed so

3:220.2 for

3:24example um so then oh what what did I

3:27write here oh this should end oh this

3:30shouldn't sorry start at 0.65 it should

3:32end at 0.65 I keep making that mistake

3:36and this starts at 0.65 and ends at 0.8

3:39yeah and this of course starts at uh 0.8

3:44and ends at the end so let's see what

3:46this looks like and wind up

3:50boom I think looks

3:52nice yeah so the windup time is to give

3:55the player enough time to react uh so

3:57they know what's happening uh let me

4:00play this also last part a bit slower

4:02maybe 0.8 as well to increase the total

4:04duration of the

4:07Montage and all right very

4:11nice so now we need some effects uh some

4:14particle effects in our Montage um now

4:18I'm going to do like this um I think we

4:20already I'm going to be using all of the

4:22particle effects from the Paragon Gideon

4:24asset pack that we're already using in a

4:26bunch of different things um and I'll

4:29leave it link for it in the description

4:30as

4:31well so I think around here I want to

4:35add let me just add two tracks so we

4:37have two notify

4:39tracks and I'm going to right click uh

4:41add notify particle effect uh I think

4:45this one is called hands something yeah

4:47emote FX hands and I'm going to attach

4:50this one to the right hand of the

4:53mesh and I am going

4:57to uh let me show you what this looks

4:59looks like so it's this effect on the

5:03hand yeah I'm going to make it slightly

5:05bigger so scale it to 1.2 on each

5:09axes and I'm going to copy it and paste

5:11it on the second track to put one for

5:14the left hand and one for the right hand

5:18this starts at

5:190.26 let's also start this at

5:25.26 and I'll attach this one to the left

5:28hand

5:30all right yeah that's how it

5:34looks and then um two things we want to

5:38do uh we want to

5:40add um a montage notify for when we're

5:44supposed to fire the projectile so

5:47probably at

5:50around right

5:53here yeah let's say around right here

5:55around

5:571.9 uh I want to fire the projectile and

6:00let's say we fire one every 0.5 seconds

6:04so let's add one here add notify Montage

6:07notify call it

6:09fire and let's make sure the time is 1.9

6:15and they're going to copy and paste it

6:16every 0.5 seconds so next one is

6:212.4 uh the one afterwards is

6:262.9 and then the one afterwards

6:29is

6:323.3 and

6:373.8 1 two 3 four five and then the

6:41Montage is over here so this is just the

6:43rest pose so has going to fire five

6:46times like this and boom boom boom boom

6:51boom now I also want to add some

6:54particle effects when we do fire uh from

6:57our hands and the partic particle effect

6:59that I'll be using so let me add it here

7:01add

7:02notify uh play particle effect obviously

7:05if you're using Niagara then use play

7:07Niagara effects and um if you have an um

7:12a particle effect like a sword Trail or

7:15something that plays on a duration you

7:17can add a notify State instead of a

7:19notify so this notify State you can say

7:21timed particle effects and then you can

7:23just drag this to when you want it to

7:25start and end uh but for my case the the

7:29particle just spawns and then disappears

7:31afterwards so it's a regular particle

7:34effect uh what was it called I think it

7:38was

7:41um cast something yeah pcore primary

7:44undor

7:46cast and we want to attach this to the

7:50um right hand as well or ref a hand

7:54doesn't matter

7:55which and uh let's see what this looks

7:58like

8:01I think it's compiling shaders still

8:04yeah but it's uh spawning here let's

8:07actually drop it down a bit on the

8:11z-axis oh I just pulled it up let's then

8:14make this 45 on the

8:17z-axis

8:19uh yeah it's not really positioning

8:22correctly let's make it

8:2520

8:28whoops

8:31how does 20 look it's not

8:37bad yeah I think it's fine just okay 15

8:41just to make it right in the center okay

8:44and then we're going to copy and paste

8:45this uh roughly around this same

8:50time as all of these

8:55notifies so then our final Montage looks

8:59some something like this and boom boom

9:02boom boom boom very nice I like it now

Adding The New Attack Function

9:07let's start using the

9:08Montage so we go to our enemies folder

9:12and go to our open up our

9:15Mage uh let's close all of this and give

9:19me my event graph all right so right now

9:22we only have one uh attack event just

9:25like we did for the melee enemy we're

9:26going to create um two or more attack

9:29events so let's comment this call these

9:34attacks and let's give ourselves some

9:37room

9:39here so the first attack this is going

9:42to be called the um basic attack I don't

9:44know if you have a better name please go

9:46do that um but yeah I'm just going to

9:48create a custom event call it basic

9:52attack and takes as input the

9:57attack Target attack Target which is an

10:05actor and then um I'm just going to

10:07replace the attack with this one so this

10:10is going to be our basic attack and I'm

10:13going to call it here as well so the

10:16default attack is the basic attack just

10:19like we did for the melee enemy now I'm

10:23going to copy all of this and paste it

10:24down

10:25here so now I have a new custom event

10:29and I'm going to call this one the

10:31barrage

10:33attack great and of course it's going to

10:36play the Montage that we just created so

10:39the um

10:41barrage attack

10:43Montage um so right now we use the same

10:46notify fire so this should um do the

10:49same thing oh don't forget to plug in

10:51the attack Target here and here it's got

10:54disconnected it's very

10:57important um well let's actually see

10:59what it looks like now without uh doing

11:02much more uh we just have to replace the

11:05basic attack with the barrage attack

11:08then uh because obviously our uh

11:11Behavior tree is just calling attack it

11:13doesn't know yet about basic and barrage

11:16but let's see what this looks like so

11:18play enemy should teleport and start

11:21doing a o barrage of attacks ooo

11:26nice I think it looks pretty cool um but

11:29we're going to replace this projectile

11:31with a with a different

11:33one um because I want each attack to

11:36have its own sort of pro uh projectile

11:38or particle effect I think then it's

11:40cooler and then the player can

11:42differentiate between

11:43them um so let's see how we can do that

Adding a New Projectile

11:47let me just uh remind you again this

11:50magic spell uses uh spawns this

11:53projectile uh so I'm going to go here

11:55and create a copy of this BP projectile

11:58dark energy

12:00um and uh make sure to check out the um

12:03how to shoot or how to create

12:05projectiles tutorial that I made a while

12:07back if you need a refresher on how this

12:09projectile system is working um but for

12:11now I'm just going to copy this and call

12:13it uh Dark Energy alt which stands for

12:20alternative um but the main difference

12:23between these two is going to be the

12:25particle system which is yeah just the

12:27effect that displays

12:29and instead of this P Gideon burden I'm

12:32going to use the P Gideon

12:35primary which looks something like

12:38this uh and it has a trail effect as

12:40well so when it moves in the world it

12:42looks uh a bit cooler than in the

12:45editor so let me reset the location and

12:49the rotation and let me scale it up a

12:52bit to like uh know

12:541.5 no 1.5 is too big let's say 1.2

12:59yeah I think that's fine and then the

13:01Box Collision uh should be a bit smaller

13:04let's say 20 20

13:0820 yeah yeah I think that's about

13:12fine um I'm going to keep everything

13:13else the same uh we can of course change

13:16the sounds the impact effects and so on

13:20and do I have a new impact effect

13:22actually I had like a

13:25muzzle yeah how about this one what does

13:28that one look

13:31like yeah it's not not a bad impact

13:35effect let's keep

13:38it um all right uh and yeah everything

13:44else I will keep the

13:46same so now back to our uh enemy right

13:51now we play the same magic spell right

13:54and this magic spell uh only

13:57fires um this

13:59uh BP uh Dark Energy now of course I

14:02could copy all of this paste it and have

14:04a magic spell that fires the uh first

14:07projectile and a magic spell that fires

14:08the second projectile but I think since

14:11everything else is common U it's going

14:13to be just a lot of

14:14duplication so what I can do instead is

14:19modify uh this uh magic

14:23spell uh to take an additional parameter

14:27and call it um alt and it's going to be

14:30a Boolean so if alt is true it uses the

14:33new projectile if alt is false use the

14:35old one uh so how do we do that you can

14:37pull off of this pin and say select and

14:41notice what happens when I when I do a

14:42select

14:43here as soon as I do a select uh these

14:46become red and it's complaining that

14:49they're no longer part of this spawn

14:50actor and the reason is because this

14:53class is just an actor uh so when I

14:56hover over it it says actor class and

14:58this speed Target and instigator are not

15:01part of the actor class they're part of

15:02the base projectile class so to fix that

15:06just unplug this pin right click here

15:09and say change pin type and then choose

15:12the uh projectile Base Class reference

15:16not object reference and now plug it in

15:20and it's not complaining

15:22anymore um all right now take this um

15:26alt plug it in here

15:29and you see uh now we have a false and

15:31true so if alt is false use the dark

15:33energy if alt is true use the dark

15:36energy

15:37alt and yeah that's pretty much it

15:41simple as

15:43that um now back in our Mage enemy I can

15:46tell him use alt for this one and for

15:49the first one I'm going to keep alt as

15:51false which is the

15:53default and we can also change the

15:55damage let's say this one does like 10

15:57damage instead of 20 you can play around

15:59with the damage numbers of course as you

16:02want um all right now let's see how this

16:04new one looks like and teleport and boom

16:10nice now this is the trail effect that I

16:12was talking about I think this looks

16:15pretty

16:16cool and uh how about the impact effect

16:20how does that

16:22look it's a bit

16:25small yeah actually I don't like the

16:27impact effect I'm going to revert to the

16:28old one uh what was it called hit

16:32something yeah this one yeah I think

16:35this just makes uh makes it a bit

16:37clearer and more

16:41impactful

16:44nice all right so now there's one more

16:47thing I want to do uh and that is right

Random Projectile Spread Pattern

16:50now uh this attack passes a Target right

16:54so if you recall magic spell takes a

16:56Target and gives it to the the um uh

17:00actor that we're spawning this

17:01projectile actor and what it does is

17:03that it checks where the target is and

17:06fires a projectile towards the target um

17:09if I do is homing then the projectile

17:12will also follow the target as the

17:13target moves like

17:17this um but I don't want it to be homing

17:21and I don't want there to be a Target

17:22either let me explain why so right now

17:26uh each one of the projectiles fires

17:28Direct ly at the player so if the player

17:30is just moving in a straight line it's

17:32very easy to avoid these attacks like

17:35this so if I keep moving in a straight

17:38line uh the Mage enemy will always fire

17:41where I used to

17:42be uh so it's quite easy to avoid them

17:46so I want these projectiles to have sort

17:48of a random pattern um uh in their

17:51firing so they should fire um in an

17:55offset maybe a bit to the left of the

17:57player maybe a bit to the right right of

17:58the player randomly so the player has to

18:01really watch where the projectile is

18:03going and then Dodge it or decide to

18:05move away from it uh you see this in a

18:07lot of games where the projectile has a

18:09somewhat random Behavior to make things

18:11a bit more interesting and more

18:13unpredictable so luckily there's a

18:15simple way we can do that we're just not

18:17going to pass a Target

18:18here and if we're not passing a Target

18:21then the rotation becomes very important

18:23because where we uh start the RO where

18:27we are starting looking at this is where

18:29the projectile will go if I have it

18:32rotating backwards then it's going to go

18:34backwards so how do we calculate the

18:36rotation needed to be a bit to the left

18:39or to the right of the target well there

18:42is a node called uh look at

18:45rotation find look at

18:48rotation and what this node does let's

18:50plug that in here is it takes a start

18:52and a Target and uh gives you the

18:54rotation needed to reach from to go from

18:57that start to the Target so the start is

18:59just where we are now so get actor

19:02location and if the target is the attack

19:06Target get actor location then this is

19:09the same behavior we had as just

19:11plugging in the Target right uh because

19:13now we just fire where the actor is but

19:16instead of firing right where the actor

19:18is what I'm going to do is

19:21add to

19:24the um what is the y axis yeah the y

19:27axis

19:29uh a random number so I'm going to add

19:33uh to the Y AIS a random float so I'm

19:35going to search for random float in

19:38range because I want to give it a range

19:40a Max and a Min so add any number

19:44between -300 and

19:47300 so this means that 3 m to the left

19:50and to the right of the player um fire

19:54anywhere in between that and then that

19:57will be the target

19:58so let's see what this looks like now as

20:02the Mage enemy teleports and

20:05fires yeah see now the behavior is a bit

20:09random obviously at some points I can be

20:11standing still and not get hit and at

20:13other times I can stand still and still

20:15get hit so as it's a bit more random

20:18it's going to keep it more exciting and

20:20interesting so the player really has to

20:21think what to do like oh oh can I go

20:23here oh can I go here and so on so I

20:26think this uh makes it a lot more fun

20:29uh perfect so now next step is to uh

Update Mage Attack Behavior Tree Task

20:34actually uh update our Behavior tree to

20:38fire uh to choose between which attack

20:40because right now we're just doing the

20:42basic attack and let me replace this

20:46with the basic attack

20:50again all right and let's open up our

20:52Behavior tree enemies

20:55AI Behavior trees BT enemy

20:59Mage and right here is where we do our

21:03attack so we run an eqs and there is

21:06inside this a Teleport and an attack and

21:08so on so just like we did for the melee

21:12attack uh if you

21:15recall uh we had

21:19a uh did I open the Mage one BTT melee

21:23attack yeah so we had this attack name

21:25and based on the attack name whether

21:27it's short range long rage and so on

21:28then we pick an attack so we're going to

21:30do the same thing for this uh attack for

21:33the Mage so first we need to create this

21:36enum with the attack names for our mage

21:39character so where is my Mage go to its

21:43folder here I'm going to create the

21:45folder call it Mage and move my Mage

21:50here this also blocking State as part of

21:53the melee let me move it

21:55here all right so this is called Mage

21:59enemy and let's open it up and right

22:01click here search for enumeration and

22:05we're going to call this eore Mage

22:09attacks open it up and we're going to

22:11add three a default a basic

22:17attack and our barrage

22:21attack nice closed it up and now in our

22:26BTT Mage attack uh we want to add a new

22:30variable here call it attack

22:33name and click on the I icon and set it

22:36type to eore Mage attacks the enum we

22:40just

22:42created um now I can do the switch here

22:46just to get this enum and do a switch on

22:48it and so on but I actually want to do

22:50this inside of a function inside of this

22:53task to make it a bit

22:54cleaner um so we can actually create

22:57functions in here so so let's create a

22:58function and call it

23:01um uh

23:04attack based on name something like

23:08that um and it needs a reference so as

23:11input it needs a reference to our Mage

23:13so do a mage ref and this is going to be

23:16of type

23:17BP oh BP enemy Mage yeah because this is

23:22the one that we can actually call The

23:24Barrage and call and so on all right so

23:28we get our attack name first we do a

23:32switch and in this switch we can then

23:35decide which attack to do so if it's the

23:38default attack we can just call um

23:41attack the one with message written next

23:44to

23:46it and our attack Target is this guy so

23:50get attack Target key get value as

23:55actor and that's our attack Target let's

23:58move it a bit up

24:02here and our basic attack is just again

24:07call Basic

24:09attack and also give it the attack

24:13Target and our barrage attack finally

24:17call barrage attack and give it the

24:20attack Target as

24:23well um all right I think that's pretty

24:28much it now let's call this back here so

24:32instead of attack I'm just going to

24:33remove this and I'm going to call my new

24:37function and I'm going to pass in my

24:40Mage ref here I think yeah we cast to a

24:42mage enemy

24:44here

24:46um yep I think that's it so now

24:51depending on this attack name we will

24:54select a different attack so let's go

24:56back to our Behavior tree and now we

25:00want this one to be one of two different

25:03attacks um now you can decide what you

25:06want this attack to be based on whether

25:08distance whether cool down uh whether um

25:12health of the player I don't know it can

25:14be anything you want could be even

25:15tokens one takes two tokens one takes

25:17one token and so on uh for me I'm just

25:20going to do a simple use case so I'm

25:21going to move this down here and create

25:23a selector because now we're selecting

25:25between two

25:26attacks

25:28so first one is going to be our barrage

25:31attack so select here barrage and we can

25:34name this also barrage

25:37attack and I'm going to copy and paste

25:40this and plug it in here and then this

25:43one is going to be our basic attack and

25:45I'm going to name it basic attack I

25:48misspelled attack

25:52here yeah all right and I'm just going

25:55to give this one a cool down of like I I

25:57know 10 seconds and this one a cool down

25:59of um yeah 2 seconds something like that

26:03so this one if it's on cool down then

26:05select this one and if this one is also

26:06on coold down then both of these fail

26:09the entire sequence fails and it goes to

26:11the evade

26:12sequence

26:14um yeah let's test it out so I start

26:17playing everything is on cool down so

26:19we're doing the evade teleport do the

26:22basic attack because the barrage is

26:23still on cool down teleport again

26:27barrage still on cool down and then

26:29barrage is no longer on cool down so do

26:33that and then teleport and do regular

26:38basic attack again both on cool down so

26:41evading very nice I think uh the enemy

26:46is looking

Handling Attack Interrupts

26:48good oh one thing actually I uh I want

26:51to do as well is the interrupt so I

26:54forgot to talk about the

26:56interrupt um

26:58right now the enemy is interruptable at

27:01any time so if the enemy tries to attack

27:03me let me show you and I uh attack her

27:07first like this let's hope I'm

27:11faster yes nope I'm not faster okay

27:14let's try it again this time I'll

27:16promise to be

27:17faster yes I got

27:20you nope I didn't okay last time and

27:25nope definitely not going to be faster

27:31but anyway um the idea is uh if the if I

27:36attack the enemy while it's doing the

27:38Montage it will be interrupted because

27:40we don't say the enemy can't be

27:42interrupted uh but for this one for uh

27:45the barrage I want as soon as the

27:48barrage starts for the enemy not to be

27:51interrupted um so to do that I can just

27:55yeah somewhere here I can pull off of my

27:58bpc damage system and say set is

28:02interruptable to false so you can't

28:06interrupt me now but very important when

28:09the attack is over I'm going to copy and

28:12paste this we want to set interruptible

28:14to True

28:16again great um now the enemy won't be

28:19interrupted while doing the barrage um

28:22and I think this makes it a more

28:24powerful spell uh I think it's pretty

28:27difficult to interrupt her anyway during

28:28that because she's firing projectiles

28:30right in your face so if you try to fire

28:33something back at her all you'll do is

28:36hit the uh the

28:38projectile watch boom

28:44yeah very

28:47nice all right and I think now we have a

28:50pretty powerful Mage

Next Up In Part 18

28:52enemy as always thank you all for

28:54watching uh and uh if you want to

28:56download the full product project files

28:58uh Source codes uh animations everything

29:00I've done so far until this part uh you

29:03can get it from patreon uh link down in

29:05the description as well as right here

29:08and um if you enjoyed this uh tutorial

29:11and you found it useful please consider

29:13giving a like subscribing to the channel

29:15uh I'm going to be uh continuing the

29:17next part with uh a few more um common

29:21attacks for our enemies uh and then I'm

29:23going to refactor our attack system uh

29:26to make it use Able by uh multiple

29:29different enemies so maybe the um for

29:32the boss fight mainly so the boss can

29:34use Mele attacks as well as Mage attacks

29:36as well as range attacks cu the boss

29:38should be able to combine a lot of

29:40different mechanics so we're going to

29:41see how we can make these a bit more

29:43scalable and reusable for multiple

29:45different cases um so uh I hope you

29:48enjoy it and I'll see you

29:52[Music]

29:56there

29:57[Music]

This transcript was generated from the captions YouTube publishes for this video. Get the transcript of any YouTube video atfreeyoutubetranscribe.com: free, unlimited, no sign-up.