3D AI Pathfinding With Navmesh (Godot tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this godot tutorial we'll be continuing our series on ai and make a character that can pathfind chase after a player control character and avoid obstacles all by itself a thank you to fellow godot youtuber ms is whatever for giving me a basic understanding of how to do this so let's open up godot into an empty scene and we're going to create an enemy character create a kinematic body node and call it whatever you want i call the enemy 2. add a mesh instance node as a child of it and in the inspector give it a new capsule shape and in the transform tab rotate it 90 degrees on the x-axis you will get a warning asking you to add a collision shape but ignore it because this only works without a collision shape click on the mesh preview window and set its radius to 1 and its mid height to 3. if you want you can add a material to it to make it a different color right click on the main note again and add a timer node as a child we'll get into this a little later but set its way time to 2 and turn on autostart now you have an enemy character next let's create a world i've got a mostly empty scene it's just an empty spatial node that i renamed to pathfinding with some lights and a camera setup for the demonstration but that's not important right click on the main node and add a navigation node as a child then right click on the navigation node and add a navigation mesh node after that right click on the navigation mesh node and i know it's a bit confusing and add a mesh instance node and give it a new cube mesh and adjust its size so it's like a platform this is what our enemy character will move around on you can also add some obstacles for the enemy to navigate around by right-clicking on the navigation mesh node and adding as many mesh instances as you want get creative finally click on the navigation mesh node and in the inspector on the right click on empty next to nav mesh and select new navigation mesh at the top of the screen click on the baked nav mesh button and a bluish-green thing will appear this is a nav mesh and it's the area that our character can move around in and run pathfinding calculations you'll notice that there is a hole in the middle of the nav mesh where the white obstacle is which basically means it's out of bounds for the enemy if you click on a mesh instance and click on the mesh button you can create a tri-mesh static body which will add collisions to your environment though this isn't really necessary for this tutorial i went ahead and added a few more obstacles then i re-baked the navmesh as you can see it changes based on the geometry of the world let's add our enemy to the world by right-clicking on the navigation node and adding the enemy scene as a child click on the navigation mesh node and click on navigation mesh in the inspector under cell change its size to 1 and its height to exactly half the height of your enemy character in my case it's 1.5 then under agent change its height and radius to match the dimensions of your enemy character rebake the navmesh and you'll see it has changed we'll add a player controllable character into the scene as well if you don't know how to make one i'll link you to a tutorial that will teach you how to make one similar to this one and with that we're ready to start programming go into the enemy scene and add a new script to the main node in it we're going to first create a reference to our navigation node which contains the nav mesh and all the world geometry by writing on ready var nav equals get parent we'll also create a variable var path equals open close square brackets this is called an array and basically it holds a series of numbers like 0 1 2 3 etc next create another variable var path underscore node equals 0. in order to calculate a path between point a and point b in a game godot will connect a bunch of nodes together in a line between the two points each node on the path has a number from 0 to infinity and this path underscore node variable just keeps track of which node we're currently on next write var speed equals 10 which is how fast our enemy will move and finally we need to get a reference to our player control character there are many ways to do this but for this demo we're going to write on ready var player equals dollar sign quote dot dot forward slash dot dot forward slash new player end quote new player being the name of my controllable character this will be different of course if you named your character something different so now let's get the enemy moving create a physics process function by writing func underscore physics process delta and in it we'll write if path node is less than path dot size var direction equals parentheses path path node minus global transform.origin so a path between two points contains a fixed number of nodes and if the current node that we're on path node is less than the total number of nodes on the path then it will run this next line of code here we're creating a variable called direction it's a vector 3 which controls which direction our enemy will move and the way this is calculated is by taking the difference between the enemy's current location and the location of the next node on the path then we'll write if direction.length is less than one path node plus equals one else move and slide direction.normalize timespeed vector3.up so if the length of our direction vector is less than one as in if our enemy character gets within one godot unit of a node then will bump path node along to the next node in the line so that the enemy knows where it should go next once it's reached the node and if it's further than one unit away then it will just move along the direction vector until it gets within one unit next we need to actually calculate the path for our enemy to move along to do that we'll create a function by writing func move underscore 2 target underscore pass path equals nav.getsimplepath globaltransform.org target pos path node equals zero i'll explain how this works in just a second but first we need to create a way to regularly update the path so that the enemy always chases after the controllable character no matter where it goes in the enemy scene click on the timer node we set up earlier and click on the node tab in the inspector right click on timeout click connect and click connect again creating a new timeout function in the code which will run whatever code is inside it when the timer times out delete pass and write move underscore 2 player.globaltransform.org so every time the timer goes to 0 it will run the moveto function which we just wrote inside the parentheses we wrote player.globaltransform.org which is the location of the player controlled character in the game up here in the move to function we have target pause this is pretty much exactly like a variable in that we can store some information inside of it in this case when we write path equals nav.getsimplepath we're telling gedo to run a built-in function that calculates a path between two points in the game accounting for any obstacles in the way in this case the enemy's current location is the start point and target pause is the end point and by writing player.globaltransform.org in the parentheses we're saying to run the move to function with the target pause being player.globaltransform.org thus causing godot to calculate a path between the enemy location and the player location and that's about it run the game and after two seconds the enemy will start chasing you wherever you go avoiding any obstacles in the way thanks for watching links to my discord twitter and computer setup down below make sure to like subscribe and blast the bell and as always have a nice day [Music] you
Info
Channel: Garbaj
Views: 27,606
Rating: undefined out of 5
Keywords:
Id: YFgrpp1fpOI
Channel Id: undefined
Length: 7min 39sec (459 seconds)
Published: Wed Aug 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.