Intro to Steering Behaviors in Godot part 1: Follow AI (tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this series is dedicated to steering behaviors I think we've covered them in the past but this one is updated for godot 3.1 and up and it's hopefully of a much higher quality I'm going to guide you through the process showing you how to make first an AI follow a point where you click with the mouse second while going to make it arrived to the point make it smoothly slow down because you can see that the previous behavior was a bit sharp and then I will show you how to make several hey eyes follow a leader follow one another using the arrive to behavior something you can use to make a party in an RPGs you can make the characters smoothly follow the leader those kinds of things without further ado let's get started in this video with the follow behavior here is how steering works you have your own character on one side and the target position on the other side the target moves right the the male's cursor can move so as it does you want to adjust the player speed to point to the target as the character has a limited velocity maybe every frame it's going to have a motion like that but now let's say that the target was there on the previous frame so the character was moving like that we move the mouse cursor progressively and it ends here our follow steering behavior is going to do this it's going to calculate the desired velocity something like that it's the direction to the target limited by the maximum speed so let's say that the maximum speed allows you to move like that roughly in one frame it's a bit fast but let's say that that's what you get the follow steering is going to calculate a vector from the players current velocity to the target velocity so it it does the difference of the two vectors giving you a vector that points directly towards that okay I'm going to make it read and then we divided by the mass so I'm going to make it smaller we add the red vector to the current velocity making the current velocity go like that of on one frame and so you repeat that every frame making the vector slowly go towards the target more and more and this is what gives you that smooth motion that is for the followed behavior the simplest one to get started you can find a start projects in our good at engine toriel demos repository in the description below head to the 2019 folder and you will find the demo in the steering fall arrived subfolder there you want to load the start version and to get a link in the description below I tend to use the command line as I'm a bit of a nerd you can use Godot from the command line if you have it available there and so here is the start project it's going to open a game scene that has three circles three static body two T's they have some collision layer sets that we're going to use in a moment a parallax backgrounds have a nice effect later when we'll add the camera in part three and if you go check the project settings the input map have created a clique in production for us so that we can detect left click easily from our code this template will help us focus on the steering behaviors and our little AI here we are going to create the first triangle that will follow the mouse cursor around so go to scene new scene and we're going to not create a 2d scene but use custom node here look for kinematic body 2d so that our triangle can interact with the world can hit and detect the circles in our game scene we're going to rename it to character follow or it could be a follow agent call it however you prefer well I'm going to drag the red triangle in there go ahead and click and drag the file here notes that on the sprites for the game are in the assets folder I'll rename it to use Pascal case as it becomes a sprite an object and reset the transform so in the inspector here you want to click that allows you to reset the value to zero so a triangle is centered to work to collide with the world a kinematic body 2d needs a collision shape so let us add it control a look for collision shape 2d in the inspector we're going to set the shape property to a new circle shape 2d then you can zoom in a bit on the view let me bring in the keyboard shortcuts here I'm going to put them at the bottom zoom in with the plus key or equal but it's the key where you have the plus button and click and drag on the point here to increase the size of the circle now select your red triangle press W to toggle the move tool and click shift and drag to constraint the no to a single axis and I'm moving it on the x-axis right there and Q key to select the selection tool the one at the top left of toolbar we are going to select the collision shape 2d increase its size just a little bit there you go this is our base scene form now well I'm going to save it ctrl s and go in the source folder where we put all our source code scenes are part of your game source code right click and create a new folder we're going to call it agents agents for AI agents it's a common expression in games in the industry and save it as character followed at the SCN with that we can quickly create a script in the same folder select the character follow node click the script + icon in the top right corner of the scene tree and let's create a script we want to set the template to empty and click create to create it ok from there we can start coding our character so we don't have the steering function to make our character follow the mouse cursor just yet I'm going to do some setup to have everything in place so that we can add the follow part at the end first we want to have a reference to our sprite to the triangle read here from the script so we can rotate it for that you create an unready variable unready is like creating a ready function like so and having some code in there it's syntactic sugar to save you a bit of time and we're going to get our triangle read note the dollar sign is a shorthand for get node so we can use the dollar sign here and subtype sprite as this node is a sprite then we want our character to move I'd say maximum speed right so the maximum speed is going to be 500 pixels per second I'm exporting the variable so that when you go back to the scene and select character follow you can tweak that value or a designer and your team can do that in the inspector with that we need our character to have some velocity the velocity is a vector that represents both the speed and direction at which the character is going to move and our steering function is going to manipulate that or it's going to calculate a new velocity that we will store in this variable we will be able to move our character with that so from there while going to create our physics process function because it's a kinematic body 2d we want to use physics process to move it reliably now our character is going to move to a target point to a target position so the first thing we'll create is a variable called target global position it's going to be the mouse's position and to get it you can call the get Mouse global position while using a global position because if we were to use get Mouse position it would give us the mouse position relative to the node so if the mouse was here it would give us a vector like so but if the mouse is here it won't give us a vector relative to the character like so as the character moves with local coordinates you're going to shorten that vector and using our follow function it's not going to be a problem you wouldn't get a bug in the movement but as we move to other steering behaviors like arrive to the distance to the target starts to matter so form consistency we are going to use global position when working with steering functions always alright so we have a global position to move to from there we are going to move the character based on the velocity at the end of our physics process here so using moving slide we are going to pass our velocity variable here and in between we need to calculate that velocity okay we're going to do those C equals something I'm going to do that for now have just that template code and we are going to code our first steering function steering are really reusable behaviors can use for all kinds of things you can move elements and make them follow arrive to a certain position avoid obstacles and all all that using vector calculations it's more reusable code like that you generally want to have some reusable library something you can access everywhere in your game Godot has a mechanism for that called auto loads auto loads are nodes or scripts that can load in your entire project in the project settings so lets us create a new script f3 to go back to the script editor file new script and we're going to save it like the path you want to go to source I recommend creating a folder called Oh to load and you're going to enter it and name it steering here and create our empty script it has to extend note this is very important and from there can go to project project settings Oh to load select the path to the script that you want to load globally in the project source folder Oh to load steering dodgy D press ENTER and click Add to add it from there anywhere in your code you can call steering like so and access it it's a steering dot whatever for now it can't do much okay so I'm gonna go back to our vector two here and on the steering dodgy D script we are going to add our follow function we are going to create a static function for that so let's create a new static function called follow it's going to calculate a new velocity based on the follow steering behavior as a word velocities are vector two's a name to the game it's going to return a vector T this is what in the optional type GD script this arrow followed by a type means it's the return type of the function we can calculate new velocity that's going to be a vector - and return our new velocity okay so we have the base for the function before we add the calculations we need a few elements a few default values so we're going to create a few constants up there because steering's work that way you need some maximum speed that you are going to use to limit your new velocity and you want to have some mass that you use to slow down the acceleration of the character you're not using an acceleration towards a point instead you will divide the steering velocity by the mass that's great a default mass constant let's set it to let's say 2 and we can duplicate the line with controlled beat to create a default max speed well going to use these in the function parameters by the way if you don't know what a static function is it's a function that doesn't have access to the state of a class it can only take values in here in the parentheses and it's going to return a new value okay it can't directly modify objects if you want press ENTER we have a few parameters to put in there so we're going to put each of them on a separate line so the line of the function doesn't become too long and they stay easy to read first we have the velocity the input velocity and the speed at which the character is moving if you want next we're going to have the current global position of that character object or agent then we need a target position so we're going to use that to calculate a desired velocity it's at the max speed by the way we have to change that something bigger this desired velocity is going to be the vector pointing from the character the agent that has to move going as fast as possible towards the target position then we are going to have a max speed value that's going to be equal to our default max speed and finally we will have a mass equal to the default mass note that you have a comma after each parameter but not the last one because it's the last one if you get an error there you know if you have a stray comma because you do placate the line you have to remove that from there we can add the calculations so while first going to calculate our desired velocity let's create a new variable called desired velocity and it's going to be equal to a vector pointing from the agents current position to its target you are going to subtract the global position parameter to target position we turn it into a direction we normalize the vector so it has a length of 1 and multiply it by max speed this is what I said this is the agent going as fast as it can so it's limited by its maximum speed towards the target then we are going to calculate a steering the steering is going to be a vector going from the character's current velocity to the tip of the desired velocity the difference between the desired velocity and the velocity and we have to divide it by the mass so the desired velocity minus velocity here divided by our mass parameter that's why mass is going to slow down the character's movement as you increase it that steering vector becomes shorter making the change in the velocity smaller and smaller so we can remove our new velocity variable a new velocity is going to be velocity plus the steering so the steering is going to slowly steer towards the desired velocity here it's going to make the velocity progressively frame after frame move towards the desired velocity because on every frame the target can change position you will see that the character well smoothly adjust to be target we can save and go back to our character follow script our velocity is going to use the follow function on our steering auto load and we have to pass in all the parameters so first the velocity the current velocity next we need the global position of this agent the targets global position the target position the enemy of the mouse in our case and note that you can use if you have the two scripts opened you can use alt left arrow to go back to the steering script so that you can see these parameters and alt right arrow to go forward in the history so that you can jump back to your character follow script next up we are going to pass the max speed and fall now we are going to leave the mass at default all right so with that moving slide is going to use our velocity one last thing we have to do is when you call moving slide the character when it's going to hit some obstacle moving slide is going to return a new velocity that it slides against the Collider that you hit so you can control click on the moving slide function to read the documentation that you can see it returns a vector to write when you have that before a method name it's it's written type and you will have in documentation at the bottom returns the linear velocity vector and all scaled if a slide collision occurred and then you can get more information in another method buts basically if you hit a circle in our project it's going to try to make the character smoothly slide against it okay so we have to assign that resulting velocity to our velocity variable I'm going to remove the lines here I have to go back to the game scene and you want to add your new character to it so look for character follow click and drag it into the scene under the game node and we can place it somewhere in the middle of the screen let's say press f5 to try the game and now you will see the arrow follows your mouse cursor it does that weird thing jiggling when it reaches the mouse this is because it's trying to follow the current position and it's overshooting it so we have to fix that another issue is that it passes through the circles it doesn't detect them so we have to set its collisions and lastly the arrow is not rotating as it follows the mouse but you can see how it smoothly steers towards the mouse it does not rotate abruptly and this is what the steering behaviors and vector calculations give you so let's fix these small issues go back to character follow first for the collisions select the kinematic body to D you want to go to the collision tab and physics body to D right here you want to change the layer so I've named them for you in this project the player has to be on the player layer and has to collide not with player but with the world the layer on which I put our circles if you go to the circles you can see that right here there are on layer two this solves the problem the second is the character jittering around the mouth position it's because it's constantly trying to move in physics process so we want to add a condition if it's very close to the mouse cursor while going to stop it so let's add a new constant up there we're going to call it distance threshold it's going to be a fresh hold under which while not going to move the character if it's less than three pixels away from the mouse cursor we're not going to move the character in this case after we stalled the target global position we're going to add a new condition here if the character is too close to the mouse so if the distance from the characters global position they can do global position that distance to note that this global position is a vector to the distance to is a method from the vector to class and the distance to another vector to to another point target global position is lower than the distance threshold we returned from physics process now we can test the game and you can see that now if the character is on the mouse it's going to stop that is the second bug fixed and the last one has to do with rotating the sprite and that is fairly easy can do it like that after the last line we're going to take our sprite that we stored in a variable at the top of the script so the sprite property that references triangle read we're going to do sprite dot rotation that's rotation and radians will be velocity dot angle angle is there again a method from the vector to class and anytime you can control click on a method to see its documentation here the vector to class and it returns the vectors angle relative to the x-axis in radians going back to character follow there we go if you do that the arrow will now follow your mouse cursor now the character is really going abruptly always at a constant speed towards the mouse cursor in the next video we will see how to make it smoothly arrive to the mouse cursor using the arrive to behavior see you there this is it for this first video but there are three more to come so I invite you to subscribe to the channel and hit that notification bell if you want to know when they come out or just if you want to get more good old game creation and programming tutorials in general from the next box it's going to get a lot more interesting growling - look at the arrived - behavior which is what we use for our hooking mechanic in our Metroidvania game project you can find a link to that in the description box below but for now I want to thank you thank you for watching until the end be creative have fun and we'll see one another in the next one bye bye
Info
Channel: GDQuest
Views: 28,061
Rating: undefined out of 5
Keywords: godot steering, godot steering behaviors, godot ai follow player, godot ai movement, godot 2d ai, godot artificial intelligence, godot tutorial for beginners, godot beginner tutorial 2d, godot game engine, game development, game programming, godot ai
Id: UWlErVIJIw0
Channel Id: undefined
Length: 25min 38sec (1538 seconds)
Published: Fri Sep 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.