Simple FPS Enemy AI in Godot - Make an FPS in Godot Part 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys Tom here and in today's video I'm gonna be teaching you how to add some simple enemy AI to your first-person shooter game so let's get started okay as always the first thing we're gonna do is we're gonna come and open our Godot engine and in the project manager you're gonna look for your FPS tutorial you're gonna click and press edit now if you haven't been following along with the previous video tutorials then head on over to the github page the link is in the description below and you can download the entire source code that you need to get started with this project right here now you'll notice if you happen following along that the scene view here is slightly different from what we left off in episode 3 and that is purely just because I've added some extra things here so that going forward we'll have a bit of a nicer scene to work with when we start in enemies and collectibles and all sorts of other things so again if you don't have this current setup here head on over to the github page link in the description below and you can get all of this ready to go once you've done that come on back to the video ok so to begin you can see we've got our player character over here and if we click on the play button you'll notice that we can look around we can move around and we can jump just like we left off in the previous video the first thing we're going to do to start adding some simple enemy AI is we're actually going to click on the player object here and we're going to go into the scene by clicking on this scene view button here we're gonna click on the root node that says player and over on the right hand side where it says node we're gonna come across to groups and we're gonna add this player to a group now I've got this in player group here I'm just gonna delete that we're gonna start our game with an estate player and I've added a capital P there and bear in mind that the capitalization of the group names is important so we're gonna click Add and that is putting this player in the player group there the next thing we're gonna do is we're gonna go back into our willed scene and we're actually going to create a brand new scene now for our enemy so what I'm gonna do is inside the res folder here I'm gonna create a new folder we're gonna call it enemy I'm going to right click and I'm gonna create new scene and again we're just gonna call this enemy it's a 3d scene and the root node again we're just gonna call enemy and then underneath here I'm going to add a couple of child nodes who can have a mesh instance now this mesh instance is going to be the capsule that represents the enemy so over on the right in the inspector we're going to click on mesh and we're gonna click new capsule mesh now again you'll notice that the capsule is rotated on its side just like the player one was so we're gonna come across to transform on the right hand side and we're just gonna rotate this by 90 degrees on the x-axis the next thing we're gonna do is we're gonna right-click add another node again a mesh instance and this time we're gonna create a cube mesh now I'm gonna do here is I'm just gonna scale this down and then scale it across and then we're going to move it up and we're gonna move it forward let's just rotate that there let's just scale this down as well now what we're doing this for is we're just creating some sort of a reference point to show us which way the enemy is facing so wherever that cube sticking out the front is that's going to show us that that's the direction that the enemy is facing the next thing we're gonna do is we're gonna add another child node and this time we're gonna add a collision shape we're going to create that and here what we're gonna do is we're going to add a capsule shape and again on the transform we're going to rotate that by 90 degrees and that just means that the player will be able to shooter the enemy and it will be able to receive collisions finally what we're going to do is we are going to change the type of this node change type and we're just going to set it to a kinematic body okay so that is our player setup also what we're gonna do actually there is on the mesh instance we're gonna come over to the material we're gonna give it a new spatial material and you'll see why we're doing this in a little while but we're gonna come down to the albedo and for the color we're just going to give it a green color so zero on our 2005 on the green and zero on the blue and that just gives it a nice green color and again you'll see why we're doing that a little bit later on in the tutorial okay so that's our enemy we're gonna come back into the world scene and we're just gonna drop that enemy into the scene now you'll notice here that he's facing the opposite direction to the player so what I'm going to do is I'm just gonna click on the enemy and I'm gonna rotate it just so that it's sort of facing in the same direction as the player and what you will notice there is when I drag that into the scene it actually nested itself underneath the player there if it does that for you you're gonna want to drag that out so it's sin underneath the world node itself and not underneath the player okay so if we play that you'll see that we have this enemy he's actually floating in the air at the moment so what we can do is we can just click on him there and let's just move him down a little bit all right cool so we have our player and we have our enemy our enemy doesn't really do anything at the moment so let's start adding some enemy AI into this okay so what we're gonna do now is we're gonna come back into the enemy scene and what we're gonna do is we're gonna create a collision area for this enemy to detect the player within and we're gonna do that in a sort of field-of-view fashion so what I mean by that is we're gonna come across to the enemy and add a child node and here we're gonna have an area now you'll notice at the bottom here it says general purpose area node for detection and 3d physics influence now the detection part is what we're really interested in so we're going to create that and underneath this again here you'll see this little warning triangle which we've seen before and it says that this node has no shape so it can't collide consider adding a collision shape or a collision polygon now whereas normally we would use a collision shape for this I'm actually going to use a collision polygon so we're going to add a child and just like it said we're going to choose collision polygon we're gonna create that and you'll notice that we can't really see it it's not really added anything what we actually need to do is we need to come over to the right hand side where it says polygon and click on where it says pool vector to array now we're going to need three array points here so we're going to click up a couple of times until we get our three points and we're going to define these three points so the first one we're going to keep it zero zero the second one we're going to say fifteen on the x and six on the y and the second one we're gonna say fifteen on the X and minus six on the y now you'll notice what that's done is it's kind of created a blue sort of collision area here and what we're gonna do is we're going to transform this we're gonna say minus ninety on the x-axis now you'll notice that's made a sort of cone shape which is perfect for what we need in terms of detecting the player within a cone of view now you'll also notice that it's sticking out the side of the enemy here so what we're gonna do is you're going to click on the area and we're gonna come to the rotation tool and we're just gonna rotate that 90 degrees and one final thing to note here is that you can see that this is sort of sticking through the enemy a little bit so I'm gonna take the move tool and I'm just gonna hold shift and drag it across ever so slightly just until it's no longer colliding with the enemy itself now the reason we do that is so that the enemy doesn't detect itself and start trying to chase itself or however or whatever AI were actually going to implement here okay so the collision area is done what we are now going to do is we're gonna come and create an enemy script so inside our enemy node we're going to right-click and we're gonna attach a script we're gonna leave all this as default so enemy Gd press create we're gonna remove all of this we don't need any of that and we're actually just going to come back into the 3d scene again and what we're going to do now is for this collision shape here we're actually going to connect some signals for this so to do that we're going to click on the area we're going to come over to the node tab on the right hand side and choose signals now you'll notice there's a bunch of signals here for this area that we can actually attach to and listen to the ones we're interested in our body entered and Exeter's we're gonna use that to tell when the player has entered the field of view and exited the field of view so we're gonna right click on this and press connect and we're just gonna leave that all this default and press connect and you'll notice that that has gone into our enemy script and it's added a function here on area body entered we're gonna go back to the enemy and again in signals on the area sorry and we're going to right click on body exited press connect and again press connect again so now we have two functions inside our enemy script one for when something enters the field of view and won't work for when something exits the field of view for now what we're gonna do is we're just going to simply print the body dot name in both of them so we're gonna say print buddy got name and actually we will just say here we'll say body done name exited and body got name entered we'll save that and now we're gonna press play and we're just going to take a look down at the bottom left of our screen there where it says output and we're just gonna check to see whether or not it detects the player coming in and out so we're going to move into the field of view you can see it says player entered and we're gonna move out and it says player exited brilliant okay so that's great but we actually need some sort of visual indicator that the enemy has now detected the player so to do that in this tutorial what we're going to do is we're going to simply change the color of the of the enemy itself now you'll notice that we made him green and what we're gonna do is we're gonna say when the player enters the field of view we're going to change the enemy to red and when it exits we're going to change it back to green again a sort of alerted stay and non alerted State to do that we're going to change our own area body entered and we're just going to call a function set color green actually sorry set color red and on exited we're going to say set color green now these functions don't exist at the moment so we need to implement them so we're going to say pump set color red and here what we're gonna do is we're going to get a reference to our mesh instance and we are going to say yet surface material we're going to get the first one which is zero index so we're going to say it's zero and here we're just going to say set albedo and it wants a color so we're going to say color and we're going to give it one for our zero for green and zero for blue we're going to do the same for green so fun set will agree again same thing mesh instance get surface material zero set the albedo and we're going to give it a color again zero for red one for green and zero for blue now if we save that and try it again you'll notice that when we move into the field of view it goes red when we move out it goes green again so red when we're inside the field of view green when we're not great okay the next step is we're going to make the enemy look at the player once it enters the field of view and to do that is quite simple we've already seen this and one of my gold or quick tip videos before in our own body entered what we're gonna do here is we're gonna actually get a reference to what body entered and if it's the player then we're gonna set a variable called target in this script so we're gonna say bar target and here what we're gonna do is we're gonna say if body dot is in group and we're gonna give it the group that we give the player which is player with a capital P and we're gonna say target equals body now all of these functions here the print and the set color red we're going to end in those so that they're inside this if statement as well and we're going to do the same thing for exited so we're gonna say if body got is in group well yeah and what we're actually going to do here is going to say target equals null so we're gonna empty out that target variable if the player has now exited we're gonna save that and then what we're going to do is we're going to say fun process so we've seen the process function slightly times before inside the process function what we're gonna do is we're gonna say if target so if we have a target then we're gonna say look at and we're gonna give it the vector that we want to look at which is target global transform the origin and we're just gonna give back the up vector three which is vector three dot I'm gonna save that one let's give that a shot so we're gonna move then you'll see that it's now looking at as it's following us wherever we go you can go closer and it's gonna follow us around and if we go outside of his field of view it goes green again and it's no longer looking at the player we can come all the way around here it's not gonna see as soon as we get into scope it's now following us again great okay next thing we're gonna do is we're gonna start to add line-of-sight into this now you'll notice if I just play this again you'll see that I've created some sort of walls here and now the reason I've done that is so that we can demonstrate sort of line of sight what I'm actually gonna do is I'm gonna come back into the world go into the 3d view I'm just gonna take this wall here if we just come over here so I'll select tool take this wall here I'm just going to move it across here so that we can sort of demonstrate line-of-sight a little bit easier let's save that and now let's go back into our enemy we're gonna come into the script and we're actually going to do a ray cast inside our process function now so what we're gonna do is we are going to say func ready and in order to do ray cast without creating a ray cast node which we could do and you have seen in a previous video tutorial that we create a ray cast node we're actually going to do it directly from code here they could just because it's slightly easier when it's a bit of a dynamic ray cast so we're gonna need to get a reference to the current physics world and the space state within that so we're actually gonna create a variable here their bar space state and here inside the ready function we're gonna say space state equals we need to do get on the scroll world dot direct space state now that space state is going to allow us to fire off a ray and see if we collide with anything so inside this if target we're also going to check whether or not we have a target and if the enemy can actively see that target by a line of sight so we're going to say bar result equals space state dot intercept break and we're now going to give that a origin position and a target position now the origin position is ourselves so global transform origin we're going to start the Ray from wherever the enemy currently is and we're going to fire the Ray to the players position to the target position with target dot local transform origin okay so now we're gonna check that result and see if it actually did collide with something so we're gonna say if result dot Collider is in group player so if we collided something and it is actually the player then we are going to make it look at the player otherwise we're absolutely going to turn the enemy green again we're gonna say set color green and we probably don't need to do this for let's just do it anyway just so it's clear what we're trying to do we're going to set the color red if we are actually colliding with the player so if we have a target and we are in line of sight of the target so we find a ray from our current position to the target if we can actually see that we've collided with the player then we are actively looking at him he's not blocked by any sort of other meshes in the way then we're gonna look at the look at the player target and we're gonna set the color to red so we're we're in high alert stairs now the enemy can see you and he's going to start doing something else and otherwise we can't see you hiding behind something and we're going to set the color back to green okay let's just put our colon on there and let's just give that a shot so we're gonna come in he's looking at us still looking at is behind a wall who can't see us anymore I know this is quite a low wall but the actual ray itself would be firing at chest height here or waist height I should say so he actually can't see us here but as soon as we come out from behind the wall you can see that he can see us again and he goes back into his high alert stairs great okay so for the final part of this video we're actually gonna make the enemy chase the player now and that is really quite simple what we're gonna do is we're gonna come into the enemy script and we're gonna create a new function here gonna say fun and we're gonna say move to target this is gonna take the Delta and inside here we're gonna get a direction to the target so just leave our direction equals we're going to open some brackets because we're gonna do some normal eyes in here so we're gonna say target dot get transform dot origin actually what we can do here is we can just say transform the origin - and we're gonna get our own transform so transform origin and then we're gonna say normalized now the reason we're normalizing it is because we just want a direction vector we don't want a vector with a magnitude we just want a vector that tells us which direction we need to look at to actually get towards the player so we've got a direction now I'm going to call the move and slide function as you've seen a million times before and we're going to do direction multiplied by speed multiplied by Delta and we're gonna give it the vector 3 dot ha now you'll notice we don't actually have a speed variable yet so we're just gonna create that at the top we're gonna make it exportable so we can change it in the inspector we're gonna say or bar speed people's I'm going to say a hundred units a second okay and now what we need to do is inside our process function so if we have a target and if we are in line of sight as a target we're going to look at the player we're going to set the enemy's color to red I'm also going to move to target and we're just going to give it the delta from the process function there okay let's save that and let's give that shot so we're gonna press play we're gonna come into the enemy's line of sight hone you can see that he now moving towards us the move and slide function actually bumped him out of the ground a little bit there which is absolutely fine it's just because we placed him slightly under the ground but you'll see now that he's looking at us and he's following us wherever we go and if we now come and run behind this wall he goes into his idle State he goes green he stops following us and he stops looking at us we come out from behind the wall and you'll see again that he starts following us there we go behind the wall and he completely stops the final final thing we're going to do here is we're gonna come back into the enemy scene we're gonna click on enemy come over to groups on the right hand side and we're just gonna add this enemy to the enemies group that we created in the previous tutorials and if we press play now and we shoot at the enemy the enemy gets killed simple as that we already wrote all of the code for doing the raycast for the weapon and damaging the damage in whatever we actually hit and making it disappear if it dies so all of that code is already hooked up for anything that is in the enemies group so by adding that enemies group there we've now added that functionality to this enemy as well and that's it for today's tutorial so we've covered some very very simple enemy AI we can do a field of view with a cone not just a circular sort of collision area we've do an actual field of view cone we've got line-of-sight so the enemy can respond accordingly whether or not we're absolutely looking at the player or if the player is hiding behind an object and we've done some very simple scripts there to make the enemy follow the player no matter he goes thank you so much for watching guys if you enjoyed today's tutorial please click that subscribe button and hit that little notification bell to get updates whenever I release a new video and if you haven't done so already please check out our disco community we have about 60 members in there at the moment people are asking questions answering questions it's just a real great place to be if you're getting into the world of game creation thank you so much for watching I'll see you guys in the next video [Music]
Info
Channel: Code with Tom
Views: 31,808
Rating: undefined out of 5
Keywords: godot, fps in godot, fps godot, fps godot tutorial, fps tutorial godot, fps tutorial, make a first person shooter in godot, make an fps in godot, make a fps in godot, how to make an fps in godot, how to make an fps game in godot, godot fps weapons, godot raycast, godot fps raycast, godot fps tutorial, godot fps, godot ai, godot simple enemy ai, godot simple ai, godot line of sight, godot field of view, godot chase player
Id: OSYehj6oa3U
Channel Id: undefined
Length: 22min 56sec (1376 seconds)
Published: Mon Apr 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.