Enemy AI: Aiming And Shooting - Godot Tutorial AI Series Pt 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're making a video game particularly if it's some kind of shooter then you might want to have some enemy characters in your game that can be of some threat to you and so in part two of our ongoing series on enemy AI in Godot I'm going to be showing you how to give your enemy characters the ability to aim and shoot this tutorial builds on top of what we've learned in part 1 of the enemy AI series which covers the use of state machines so if you haven't already seen it go ahead and watch it now before moving on with the rest of this video so let's open up Godot and take a look at what's going on like in the previous video I'm using this dummy model that I created for this demonstration and again I'm trying to teach broad concepts here that apply to many different situations and so it doesn't matter what kind of model you use if we take a look at the script for the model you'll see that we've got a very basic state machine already set up and mostly ready to go inside of this enumerator we've defined the various states that our model can be in in this case it's idle and alert and like in the previous video we've got a variable called state which keeps track of which state our model is in right now it's set to idle we also have a reference to the raycast sticking out of our models chest and if you want to give your character the ability to shoot as we'll learn about later in this video I suggest you go ahead and add a ray cast to your model as well we also have a reference to the animation player that controls our models animations which we talked about in part 1 of this video series the last item of note is the actual state machine itself here we've given instructions for what our model should do when in a particular state for example in the idle state it will play the idle animation and in the alert state it will play the alert animation once again make sure you've watched my video on state machines if you don't know what I'm talking about the first thing we're going to do is make our enemy turn and face the player if the player gets within a certain distance to the end me and to do that we're going to right-click on the enemy's main node we're going to select add child and we're going to type area in the search bar click on the area node and press create I'm going to double click on the area node and rename it to site range then right-click on site range select add child and type collision shape in the search bar click on the collision shape node and press create over in the inspector next to the shape property click on empty and select new spheres shape in the menu that pops up click on spear shape change its radius to something a bit bigger in my case I increased it to 10 so now we have an invisible sphere shaped area around our enemy that represents how far away you can see things we want to make it so that if our player enters into this area then it will put the enemy in a state of alert and turn to face the player to do that we're going to click on site range and in the inspector click on the node tab right click on body entered click on connect and in the window that pops up click on the enemies main node and press connect this will create a new function in your code that will activate whenever a body enters into the sight range area after that click on site range again go back to the node tab right click on body exited and repeat the same process one more time now you have a separate function for bodies entering and bodies exiting the site range I'm going to move both of these functions up above our process function just because I think it looks nicer the next thing we're going to do is go to the scene for our player character and I'm assuming you've already got your own player character to use but if you don't you can watch one of my videos on how to make one anyways we're going to click on the main node for our player and in the inspector under the node tab click on groups and type player in the box and click Add this will put your player character in a group called player and I'll explain why in just a second next we're going to return to our enemy scene and go back into the script in the body entered function we're going to delete paths and we're going to write if body dot is in group player and remember player is the group that we just put our player character into then we're going to write state equals alert then in the body exited function we're going to write state equals idle and so what's happening is that if something enters into the enemy's sight range if that thing happens to be in the group called player then the enemy will enter into the alert state and if something exits the sight range then the enemy will return to the idle State the reason why we check for the player group is because we only want the enemy to become alert when the player enters its sight range and not when walls or other non player obstacles enter it so now we're going to make the enemy turn to look at the player and there's a lot of different ways to do this but I'm going to use a simple solution that doesn't require any complicated math the first thing we're going to do is right-click on the enemy's main node and add child type spatial in the search bar click on the spatial node and press create next double click on the spatial node and I'm going to rename it to eyes then we're going to return to our script we need to create a reference to our eyes node that we just created and so up in the variables we're going to write on ready for eyes equals dollar sign eyes this is so that we can make changes to the eyes node within this script we're also going to write VAR target which is going to be the thing that our enemy turns to look at as well as Const turn speed equals to turn speed will control how fast our enemy can turn which is something we'll implement very shortly so now under state equals alert we're going to write in target equals body so whenever an object enters into the sight range that thing is referred to as a body and so if that body is in the player group then the target which is what the enemy will look at will be set to that body next we need to give the enemy instructions for what to do once it enters into the alert state and so under ap play alert we're going to write eyes look at target global transform origin vector three up and so when our enemy is in the alert state the eyes node will look at as in it will literally rotate itself to face the targets location in the game world vector to read up is there to tell the game which direction is up so that the eyes node will know how to orient itself now keep in mind this will only rotate the eyes node but it won't rotate the enemy itself at least not yet to do that we're going to right rotate underscore Y egg to red eyes that rotation Y x turn speed what this will do is it will apply a rotation to our enemy on the y axis which is the vertical axis and this is done in radians which can be a bit hard to understand and so we used egg to read to convert radians to degrees which are much more intuitive and the amount that we rotate the enemy is equivalent to the amount that our eyes node has rotated we then multiply that rotation by our turn speed which will either increase or decrease the speed at which the enemy turns now before we try it out I'm going to go into debug and tick the box next to visible collision shapes to make it easier to see what's going on but you don't need to do this in your own project when we run the game when we enter into the enemy's sight range the enemy turns to look at you and we'll continue to track you as long as you stay inside the sight range once you leave the sight range the enemy then returns to the idle state so now let's get the enemy to shoot at you the first thing we're going to do is right-click on the enemy's main node select add child type timer in the search bar click on the timer node and press create I'm going to double click on timer and rename it to shoot timer click on shoot timer and in the inspector I'm going to set the wait time to 1 second and I'm going to make sure that one shot is not enabled then we're going to click on the node tab click on signals right-click timeout and click on connect click on the enemy's main node in the window that pops up and press connect this will create a new function in your code that will trigger when the timer counts down to zero again I'm going to move it up above the process function because I think it looks neater next we're going to go up to our variables and we're going to create a reference to our shoot timer by writing on ready var shoot timer equals dollar sign shoot timer then in the body entered function we're going to write shoot timer start and so when a body in the player group enters the enemy's sight range it will cause the shoot timer to start counting down then in our body exited function we're going to write shoot timer stop which will cause the shoot timer to stop counting down when a body leaves the sight range and finally we're going to go into our timeout function we're going to delete pass and we're going to write if raycast dot is colliding so when the shoot timer times out we're going to check if the raycast sticking out of our enemy's chest is colliding with something then we'll write var hit equals recast get Collider so if the raycast is colliding then whatever it has collided with we'll be given the name hit then we'll write if hit dot is in group player print hit so if the object that the raycast is colliding with is in the player group then the game will print hit in the console so now when you run the game if you enter the enemy's sight range it will turn to look at you and if the raycast is colliding with you then every second it will register a hit and print it in the console if the raycast isn't colliding then it won't register a hit and nothing will get printed in the console and of course when you leave the sight range the enemy stops shooting and that's where we're going to end today's tutorial now that the enemy can aim and shoot we can then expand on it with more complicated behavior such as making the hits actually do damage to us but in this video we're just going to keep things simple anyways thanks for watching the video come join the discord if you want to chat with a cool community of game developers and as always make sure to like it subscribe it share it bail it and comment it thank you have a nice day
Info
Channel: Garbaj
Views: 30,350
Rating: undefined out of 5
Keywords: godot, godot tutorial, godot3d, godot 3.1, godot 3.2, garbaj, how to make video games, how to program, how to code games, game dev, game development, game dev tutorial, unity, unreal engine, ue4, ai, artificial intelligence, enemy ai, shooting, aiming
Id: wFyQLwWL0q4
Channel Id: undefined
Length: 11min 39sec (699 seconds)
Published: Wed Jun 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.