Unity 2D Pathfinding with NavMesh tutorial in 5 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to root bin studio in this tutorial I will show you how to implement 2D pathfinding in unity starting with a brand new Unity project the first thing we will do is to download navmesh plus from GitHub there's a link to that in the description below click the green code button and select download zip unzip the file you just downloaded and open the folder called navmesh plus Master inside it you will find a folder called navmesh components that's the folder we want to drag and drop into the unity editor into the assets folder then right-click the unity hierarchy to create an empty game object that we will call environment right-click environment and create a new square and call it walkable this will be the ground where our player and enemy can walk give it a color if you want and resize it to your preferred size now with the walkable game object selected attach a navigation modifier to it it is a script from the navmesh plus package that we downloaded in the beginning then create a new empty game object called navmesh select add component and add a navigation surface to the navmesh object then we add another component to it called navigation collect sources 2D click rotate surface to x y and then press bake in the navigation surface component now you should see our nav mesh in the scene view which is the area where the players and enemies can walk around let's now create a new 2D Square as a child of the environment object and call it obstacle give it a color if you want and resize it this will be the non-walkable area in the game that the AI has to find a path around set the order in layer to 1 in the Sprite renderer to make sure it gets rendered in front of the ground position it wherever you want and add a navigation modifier component to it then select the override area check box and select not walkable in the area field duplicate it to add as many obstacles as you'd like to the game now we go back to the nav mesh in the hierarchy and press bake again remember that you will always have to go back here and press bake when adding new obstacles as you can see we have re-baked our nav mesh and it successfully understands where the walkable area is now it's time to create our player and the enemy create a new 2D Circle and call it enemy then create another 2D Circle that we call player set their position wherever you want and give them your favorite color now it's finally time for some coding create a new folder called scripts and create a new c-sharp script inside it called enemy in the enemy script create a new serialized field transform Target then create a new field of type navmes agent called Agent don't forget to add using unityengine.ai at the top of the script then in our start function we type agent equals get component navmesh agent agent dot update rotation equals false and agent dot update up access equals false in our update function we type agent.set destination target.position this will make our enemy using two D nav mesh pathfinding try to follow the target all the time now back in the unity editor since we created a 2d project we will need to download the unity AI navigation package from the package manager select Unity registry and search for navigation install the package called AI navigation by unity when installation is complete we can add a navmesh agent to the enemy game object also make sure you add the enemy script to the same object select the player and the enemy in the hierarchy and set the order in layer to 1 to make sure they appear in front of the ground then drag the player object into the Target Field in the enemy script that is attached to the enemy object that will make the enemy keep following the player all the time now when we Press Play We will see the enemy following the player if we go to scene View and move the player around the enemy keeps following and that's it for this tutorial on how to implement 2D pathfinding in unity in only 5 minutes don't forget to leave a like And subscribe for more short Unity tutorials like this see you in the next one
Info
Channel: Rootbin Studio
Views: 6,109
Rating: undefined out of 5
Keywords: Unity, 2D, NavMesh, Pathfinding, Tutorial
Id: HRX0pUSucW4
Channel Id: undefined
Length: 5min 12sec (312 seconds)
Published: Fri Jul 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.