Enemy A.I. Shooting Projectiles (+ Damage) | UE5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
tutorial we will create a shooting enemy which will shoot projectiles at us and we can take damage and we can lose the game so game over when our health reaches zero okay let's do this let's create a new blueprint at first so click here do empty blueprint class and this will be your character so click character and let's name this baby underscore AI enemy save okay let's choose something to represent the enemy later on you can switch it with the model of your own click add and let's start with a sphere for example and let's let's fit them into the capsule here this is our enemy for now okay just something to represent it we're going to play this enemy inside of our map so here it's in my blueprints folder right here and then I can drag this into level hooray we have our enemy right here so back into our enemy we're going to add components so let's look for pound sensing and click here Now power sensing is a component and you can really see this this lines around my enemy and now also in my map you see these lines around my enemy now there's a lot of vision because my player will be able to sense my character and when he sees him he can attack him but you can do then well if you like this distance you can leave it as this but um let's for this example set this all to 600 sideways also so this is only when you come really close that he will shoot at us so when I walk into the range it will shoot and later on you can change so these are very low settings you wanted bigger for most games but this is just for tutorials sake you can increase this back to default 2400 if you like now bouncing here is selected go down here and in event on C Pawn and upon that he will check for I'm in the first person Temple by the way so then cast to first person character if your the character is named otherwise you choose the name of your character in my case it is this one then we're going to do drag from this pin sets vector rotation and the actor will be itself because this will be my enemy right here I want them to rotate it towards me before he shoots so for the new rotation that we are going to do drag off the spin find look at rotation and we find two points the starting point will she will be my enemy and the Target location will be my character right here so let's right click gets extra location which will be itself and connect it to the start transform and then the Target location well we need to drag this out a bit let's move it a bit will be from here get actor location and then you will find the the location of those and then he will be able to rotate towards there okay then we need to create a variable so here on the variables add one can attack question mark and it's a Boolean so this is fine hit compile at first he will be able to attack us so set this to true and compile once again be click to add in a branch because only if he is able to attack us will he attack him so this will be the condition so from true spawn exit from class an actor that I'm going to spawn from class the projectile BP first person projectile and let's look at it in the templates you see this little ball [Music] so here you have all the options under projectile movement you can increase the speeds you can also set the gravity to zero if you want to fly it straight through or even increase the gravity this is how fast the ball will fly this is already pretty fast but you can double it to 6000 for example you don't really have to mess with this but he's going to fire the ball you can also replace the sphere the little tennis ball here with the bullet so you can import static mesh or change it to anything you like right now for this tutorial I'm going to choose this ball but you don't need to do anything with this just is going to shoot his boss then in the viewport I'm going to add an arrow so here back to my BP anime AI let's add a new Arrow and let's Zoom this let's name this to spawn location or spawn projectile location and we see a new error writer so let's move this one in front of our character okay so this is the place where the balls will spawn so here's fine now compile will give an error because we need to uh we need to do some magic right here okay so now from here we're going to drag this spawn projector location drag it in here and then get transform and get worlds transformed yeah right here this is the one and move it into the spawn transform and gone are the errors the transform is when you go back here is location rotation and the scale of this place so right now we know where to spawn it and this projectile will fly straight towards our character at least that's what we're going to set up so let's take some damage on our enemy from here drag off apply damage so this is the one that we are going to need the damaged actor will be this one or first person character so from here so drag off and then plug this one in here let's give him one damage for now and rest is all fine and after the damage is applied we no longer want to be able to attack immediately we need a little delay before we can attack again so d-click to add delay notes um let's set half a seconds and then he can attack again so also I'll drag this out I set this back to true so what's going to happen now when my enemy can now see here the pawn sensing when I'm in this radius of the pawn sensing my looks at my enemy he will rotate towards it then he can attack if this is already on cooldown then he will spawn the projectile this will apply damage towards our player then we can't attack for half a second and after half a second we can attack again and then he has to detectors again and then well The Show Goes Around to once more okay what we're then going to do is we're going to go in first person blueprints to our first person character and we're going to right click event any damage because now we are going to program the logic to take damage now I already created this variable Health let's delete so you can follow along so on variables click let's name this to our health for our player's Health let's set this to a float let's compile let's give him 10 Health to start out with okay so when it takes damage we need to set a health and it needs to be lower because we'll take damage so let's get our health first then minus to subtract we get our health and we do minus the damage and then that will be our new health let's organize this maybe something like this will be more cleanly yes this is a way better and let's show this on screen so print screen and let's show the health on screen and also be click to add a branch that is the condition let's get our health once again and if it is less or equal to zero then we lose the game for example so let's print on our screen game over but this can be editing you can create a widgets with game over you can restart a level or anything you would like from this spin now when I start the game I will walk into the range that I set up and he will shoot at us but let's make sure yeah so I'm going to rotate it a bit so he can see us because when I'm behind the player I set it up to 180 degrees you won't see me so now he will be facing towards me let's hit play so nothing is happening right now this is what I want because I'm outside of the range now let's step into the range you can see he's shooting at us and here in the left top corner you can see the damage 3 2 1 and then game over because my health is below zero and now when I step out again he stops shooting inside rage he starts shooting once more and then let's open this one up again Ctrl e go to viewport let's select bounce sensing and let's increase these values once more like how they were before or we can even increase this more this is the sensing interval you can also make this interval faster so he will recheck um if we are there much faster I leave this as this right now and then you can see now at the start I'm already into his vision so now let's play and he's already shooting us then I can also move our guy let's say here behind the wall and let's rotate him towards there so when we come around to all he will see us hit play right now nothing is happening and then boom there is our enemy and he's shooting at us when we walk by now our enemy is standing still so this is perfect for turret placement or something but if you have a moving enemy you have to do one more thing so here click here volumes and I have the mesh the nav mesh bounce volume this is the region where the where the enemy can lock up on and now when I hit p on my on my keyboard you can see the zones where my AI could work but right now he is standing still so uh this is what I wanted for me so right now you can have a battle you can shoot him he can shoot you but he is able to do damage to us until we lose the game don't forget to grab the free Game Dev toolkit in the description down below this video you will get my free ebook on how to get better at game development a free game design document to plan out your projects and a free tutorial Series where you create a platformer game from scratch and those who are hungry for more I offer a sneak peek and overview of my new premium course the unreal fault where you learn how to create beautiful levels create a game from scratch to finish and how to set up boss fights and create Dragon fights with different attacks including fire breeding so if this sounds like a good deal to you I see you there click the link and grab your gamedev toolkit now while it's still free cool
Info
Channel: Buvesa Game Development
Views: 12,964
Rating: undefined out of 5
Keywords:
Id: RKHM862PWKU
Channel Id: undefined
Length: 11min 46sec (706 seconds)
Published: Sat Apr 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.