Simple 2D Pathfinding In 4 Lines Of Code! | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another episode of unity so today actually i got a fun little thing to show you guys 2d path finding now originally when you had to do pathfinding in 2d you had to do something like a star okay and that's like a huge complex thing that people go literally to college to learn and stuff like that but i came across the other day this amazing github repo that you can just quickly uh you know go and download um it it's it's basically what it does is it takes unity's 3d nav mesh and forces it into like a 2d scaling kind of it's really cool really easy to implement you literally just go right here to this little green button here and you click on and you click download the zip file and to which you'll get like a little zip file holder here all you need to do is extract it to your desktop so i have the zip file or the zip file yeah it's like right here it's already extracted you can open it up okay you see all the stuff in here so now what you want to do is you want to open up unity project and i'm going to quickly get this all opened up and then you want to just you know in the unity hub you go up here to new and just click on the newest version whatever that may be mine is 2020.1.2 f1 and then just go ahead and make it 2d and then name the project whatever you want to call i'm just going to call this one test it's not really too important just to be clear you don't have to make a new unity project to implement this nice little nav mesh thing if you already have an existing project with you know something you want to have pathfinding with okay don't worry about it all you have to do is just follow the steps i'm going to show you anyway i'm just making a new project for the people that might want to like test it out before they try to put it in something or see what it is um so don't worry about that you can you can follow the steps almost exactly how i'm gonna tell you no matter what your project is okay so here we are in the scene as you can see it's just a normal little 2d scene for unity um all you have to do to install this package is you just drag and drop the folder that uh we downloaded so this folder right here you just drag and drop that into the unity itself or well in this case you're going to want to show it in the explorer all you have to do to do that is you right click go to show and explore right here make sure you right click on your assets folder or something shown explorer and you just double click the assets folder you can just go ahead and make a folder in here called like path finding i guess it doesn't really matter what you call it could also call it packages just open that all you have to do then is just either drag and drop it or you can copy paste it i'm going to copy paste it because i use the pathfinding for a lot of different things okay so now it should be implemented in the unity unity's gonna take a second to go ahead and like import it all as you can see it's doing that right now it might take a little bit um got a bunch of fancy stuff it's gotta do okay so now we have this pathfinding thing okay and it's got a bunch of you know licensing or whatever uh as far as i know this thing is free to use um so how do you implement it well first off you're gonna wanna make a tile map which is really easy to do you just right click go to 2d object and you just click tile map we're going to call this ground just rename it the ground and then you're going to want to duplicate it and you're going to rename this to walls then what you're going to want to do is make sure you open up a tile palette window which you can do by just going to window 2d tile palette and you just drag that and dock it someplace it doesn't really matter where just go ahead and create a palette here i'm not going to name the palette it's not too important where i put this i'm just going to plop it down so now we need to drag in some sprites um you can make your own sprites or just go find something i'm just gonna import a sprite thing that i already have quick okay so i have my sprites right here once you get them imported okay i'm sure if you're looking up pathfinding here and you know how to do all this but just in case just go ahead and make sure you got all your pixels per unit and your sprite mode and all that stuff set up you can uh make sure if you're doing pixel art you make sure you turn compression to none and then you do a no point filter and you click apply that way the pixel light doesn't appear blurry or whatever um and then i'm also going to go ahead and cut this by cell size and these are 32 by 32 pixel art stuff there we go click apply bam okay so now we got some stuff i'm just going to drag these images in here um i'm gonna take the blank one because that's gonna be just the regular ground and then i'm also going to drag in well hang on you know i'm gonna find a wall image quick make this a little bit easier to understand okay so now i have the ground right here which is the green simple square and then i got this cool little wall image from one of my other previous games um okay so now what we're gonna do is make sure you have the active tile map slick selected or whatever or whatever you know um this this is for people who haven't made the tilemap thing before um if you have you can probably skip ahead in the video or just continue watching anyway we're gonna paint the ground tiles so we're just gonna make this little box selection here and just drag that like this we'll make it kind of big i guess we'll do something like this and then we're gonna swallow like switch to the walls the walls tile map is gonna represent stuff that the ai cannot walk over so let's just place some stuff around like this maybe like one little blockier view there something like that something like you know that the ai has to navigate around okay so you want to go ahead and create an empty game object rename it nav mesh and then reset the transformers right click click reset that's all you have to do then you want to make sure that you go and add component and you can just type in nav mesh surface 2d and that is a part of the github thing that you just downloaded and you want to add that to this game object um don't do anything with this quite yet we need to do is go to these ground and walls tile maps that you made um i'm just going to highlight both of them and just add the same component to them both both of them get a navmesh modifier that means it's basically just going to include it in the navmesh what you're going to want to do is click override area and just make it so you mark ground as walkable and then you go to the walls do the same thing except you want to make walls not walkable now what that's going to do is when you use the nav mesh and you click bake it is going to allow the big thing to well do its thing kind of but i made one simple mistake there is a weird thing you have to do you have to rotate this object negative 90 degrees it has to be in the x direction so right here you just go to rotation x negative 90. that's all you have to do rotate negative 90 and then click bank now as you can see it kind of looks like a whole world of up whatever that was i don't know all you have to do is just if you want to get rid of it all i did was check both these boxes and click bake then i unchecked both of them and i clicked back again now it doesn't look so wacky i don't know what that was but anyway so now you can see this as it's basically just a giant like you know nav mesh that you'd see in normal 3d um the agents will be able to walk around this so now i'm going to show you guys how to go ahead and make something traverse this so-called nav mesh um we're just going to go ahead and create a 2d object to create a sprite we're just going to name this like i don't know ai sure doesn't really matter reset the transform um reset and then we're gonna give him a little sprite we'll give him a knob yeah i like knobs okay all right what can i say um we're gonna make him i don't know three times as big something like that and he should be where is he why is he not appearing in the scene he should be appearing oh it's cause he's hidden ah that makes sense yeah he was hidden behind the camera and stuff okay anyway we're going to give him the nav mesh agent component and then we don't really need to do anything with this okay i mean over time you guys are going to be tweaking these values but i'm not here to really go through that i'm just here to show you guys how this works and very simple stuff so we're just going to do like simple ai movement that's what we're gonna call the script that we're gonna make on him so go ahead and make a new script call whatever you want mine's just gonna be called simple ai movement um it should just get added straight to him just like that good now if you were to look at the github page and i just had it open thank you thank you visual studio for ruining it if you were to look at the github page you can you got to scroll down and it it says that there is a little problem with the nav mesh asian itself so you have to make sure that you in the start function of every ai unit or whatever you put this code right here so we're just going to copy paste this into the start function of our thing and what it basically does is i'm assuming it stops the 2d like image from rotating rather than just going towards whatever um so you just want to copy paste that in you're also going to want to do on the top here you want to include the library the unity engine dot a i and that's going to get rid of our little error there for the navmes agent um so now what we're going to want to do is have some sort of target it doesn't really matter what so we're going to do a serialized field for transform we're going to do target and i believe that's all we need to do now just an update function this is not this is not uh like an actual good um efficient way to do this but i'm doing this just to show you that this is how easy it is to set this up so now what we're going to do is we just want to use the variable agent which we apparently need to quickly make a agent so nav mesh agent we're going to call this agent not agreent agent and then we're just going to replace this right here with agent because we want to make sure we cache that we don't want to be doing git component every frame that's not good anyway so now we have this so we'll do agent dot set destination and then we're just going to feed in the target so you just do the target dot position so now this ai is going to be moving are you guys ready for this this is fancy stuff we just did it we got a moving ai pathfinding in like four lines of code well unless you want to count all of it so it's one two three four five six six lines of code but it's basically four um hopefully this works you're going to want to make another object in here just an empty game object resets transform and then click on your ai and just drag in the object that you just made that's basically your target guide you're going to be dragging around um fingers crossed this hopes first try hopes first try yeah works first try yeah okay here we go all right let's see if this works um oh don't maximize on play okay so i'm gonna click on the game object apparently he's not moving but if i were to move it as you can see this thing is following my cursor around and he navigates around the walls very smoothly it runs very efficiently as you can see he stops turns around goes around the wall nice and smooth and that is honestly some of the simplest pathfinding ai you could ever implement into a 2d space let me tell you trying to learn a star is not the most efficient thing in the world to do with your time so don't ever feel ashamed to use someone else's code if they put it out for other people to use don't ever feel ashamed to do that it can help you a lot in the long run especially if you're just looking to make games for fun anyway i hope you guys enjoyed this quick video um leave a like and subscribe to see more and i will see you in another youtube
Info
Channel: TheZBillDyl
Views: 49,988
Rating: undefined out of 5
Keywords: pathfinding algorithm, pathfinding unity, pathfinding unity 2d, pathfinding ai, unity tutorial, unity 2020, unity 2d tutorial, unity ai, brackeys 2d, brackeys unity, brackeys c#, brackeys unity tutorial, brackeys beginner, code monkey grid, code monkey unity basics, codemonkey unity, code monkey a*, code monkey ai, code monkey pathfinding, code monkey beginner
Id: W-NIYi1t16Q
Channel Id: undefined
Length: 11min 13sec (673 seconds)
Published: Tue Sep 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.