Intro to Steering Behaviors in Godot part 2: Arrive To AI (tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the previous video link in the description we saw how to make our agent follow the mouse cursor using the follow steering behavior here we are going to make it arrive smoothly to the cursor let's get started I'm going to create a new character so that you can see the difference so we'll have a follow character here and let us create a copy of the character follow scene right click in the file system tab let me reset that you want a good source agent character follow right-click select duplicate and we're going to enter character arrive to it's going to arrive to a target with that double-click the scene to open it let's rename it to character arrive to here as well and we want to create a copy of that script click to open character follow and file save as we are going to save it as character arrives here once you've done that you want to make sure and the file system tab to drag the new script on to your character arrive to here to replace character follow well starting with the same base because arrive to is quite similar to our previous behavior the way it works is it's like follow but when you enter in a circle around the target in a certain radius you slowly scale down the velocity while going to duplicate our distance threshold constants that while still going to use to stop the character definitely to add one called slow radius let's make it let's say a $200,000 to load to add our right to function if you press ctrl alt o you can quickly open a script in your project so we're going to open steering GD yeah and after follow we need to add a new one called arrived - you know what we can copy the follow function because it's going to be quite similar to copy it I'm going to paste it down there I'm going to move the view and rename it to arrive - we want to add one parameter I typically add it before the mass called our slow radius and when I'm going to add a new default slow radius constant at the top don't forget the comma at the end of the line so I'm going to copy that name go back at the top of the script duplicate the default maxscript constant we've controlled be and replace it with our slow radius the default one let's make it let's say 200 pixels whatever go back down to the arrive to function and we are going to add the missing bits the missing part so with this one we want to calculate the distance from our agent to its target so the distance to the target going to call the variable to target is going to be global position dot distance to target position and this is similar to what we did in our character in its physics process function and we still calculate the desired velocity the exact same way and from there we want to reduce the desired velocity if the distance to target is within the slow radius so we can do it like that can go if the distance to the target is lower than the slow radius we're going to reduce the desired velocity so we're going to multiply it by we're going to take a ratio to target / slow radius like that and then you can offset the value a little bit so this is going to give you a ratio between zero and one if you use that value it's the pure arrived to behavior but it tends to slow the characters so much it starts to move really slowly to the target so I like to offset it by a small value for example times zero point let's say eight plus zero point two when you do that you make this value to target / slow radius a value in the range zero zero point eight and then you can add zero point two to make it in the zero to one range so from there well scaling down our velocity vector based on the distance to the target and that is it really that's all that we need so save go back to our character arrive to script and where we used the follow here method we are going to use arrive to instead now we have to add the slow radius parameter after the max speed with that we have to go back to our game scene here and click and drag character arrived to in there or you can drag it on the canvas I'm going to move it with the two characters away from one another like so press f5 to try out the game and you're going to see they move at the same speed and the same way again the behaviors aren't kind of the same but the arrive to character smoothly arrives to the cursor right if we increase our arrive radius so you can go back to your arrive to character we can change our slow radius into an exported variable so let's move it down to lines with alt down going to change the constant here by export variable slow radius I'm going to select slow radius to remove I don't want it to be uppercase that suggests it's a constant as it's not a constant anymore control all go to the replace field and slow radius and replace all instances okay now going back to the game scene we can increase the slow radius to say 500 I'll hide the character follow it's still there it's just not going to be visible so there you can see how the character starts you slow down from far away you can see how it does maintain a bit of momentum a bit of speed right if you go back to your steering function here steering behaviors and we remove the time zero point eight plus two press f5 you can see how much the character slows down now it's little too much right see you can really visualize it this way that's why I recommend you to add that little multiplier and the to set the desired velocity to a minimum that's it for this one it's long enough already in the next video we'll add the ability to click to move to a target to animate a little crosshair to start to pimp up the game a little bit might add the camera and then in the following one we'll make several AI agents follow one another start to not follow the mouse but follow characters for example could be your player character but that said thank you for watching be creative have fun subscribe and see you in the next one bye bye
Info
Channel: GDQuest
Views: 8,100
Rating: undefined out of 5
Keywords: arrive to steering, godot steering, godot steering behaviors, gdquest steering, arrive to behavior, godot 2d ai, godot steering ai, steering behavior, gamedev, godot tutorial for beginners, game creation, game development, godot 3 tutorial, godot ai
Id: fnkgBaWKZNU
Channel Id: undefined
Length: 8min 33sec (513 seconds)
Published: Mon Sep 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.