Simple 2D Pathfinding In 4 Lines Of Code! | Unity Tutorial
Ойындар
Join the Discord (it's fun): / discord
GitHub Link: github.com/h8man/NavMeshPlus
Leave a Like and Subscribe to see more!
Follow my Twitch and join the stream party: / thezbilldyl
#Unity #Programming #GameDev
Пікірлер: 182
Nice of the guards to let you record tutorials in prison.
@TheZBillDyl
3 жыл бұрын
I’m in a college dorm room haha!
@drakorez
3 жыл бұрын
@@TheZBillDyl So he was right lol
Thank you SO much for creating the little review portion for people who haven't made tilemaps before. As a new unity creator, I found that very helpful
@TheZBillDyl
3 жыл бұрын
You’re very welcome!!
This is exactly what I was looking for. Thanks a lot!
This tutorial saved my life, thank you. This saves me the anguish of having to learn A* math to write my own pathfinding system, pay for a commercially made one, or try to wiggle my way around free versions that don't take agent radius into account. Now I can actually get back to the rest of the game mechanics after a week of trying to get a dude to walk around a fence.
@lukasvolcik5109
2 жыл бұрын
Truuue. Why is it easier to do pathfinding in 3D rather than in 2D lol?
Thank you for the assurance bro, really needed it.
Normally I wouldn't comment on tutorials like this but man this srsly helped me out thank you so much!
It was just what I was looking for, short and a good explanation.
Wow! Just what I needed. Thank you man, you're a savior.
@TheZBillDyl
3 жыл бұрын
You’re welcome :)
Great content, helped me a lot, keep it up!
Thanks for this video! Throughout the years I've implemented A* myself, then years later I just pulled in some non-Unity-specific pathfinding libraries. But once this is set up it's much simpler and hands-off, and I'm hoping it it's as performant as the 3d nav mesh tools as well.
Thank you i was making a game jam and it was all doing well, i needed to do a navmesh but I spent hours trying to figure out how i can do this, i tried brackeys navmesh, but the agent was stupid it could pass through objects and with that tutorial you saved my life mate👊
Thanks for the video! Is there a way to change the Min Region Area on this 2D NavMesh surface? I tried going to Window > AI > Navigation and changing it there (like with a 3D NavMesh), but it didn't seem to update the 2D one in my scene when I re-baked.
Great Man!!! just what i need!
Excellent work
i would enjoy seeing more of these unity tutorials. Im trying to make a game in Unity for my CS class and i have no idea what im doing so more vids like this would be much appreciated
@TheZBillDyl
3 жыл бұрын
Do not worry! I will continue to make these occasionally! Whenever I can think of something to make a tutorial on.
@gamerninjask6746
3 жыл бұрын
@@TheZBillDyl i need help how i can make bot detect an gameobject and not only tilemap
@piranhapants7290
Жыл бұрын
@@gamerninjask6746 1 year later- add the object to the same layer / tag as tile map
you made that really easy , thank you
Very helpful! Thank you!
Anyone know what the asset pack is for the environment that was on the thumbnail for the video?
Does it work on normal 2d view instad of top 2d view ?
It's very useful! Thank you!!!!
Ok this is cool. Can this astar pathfinding system work for obstacles that are not based off of squares or a tilemap? Like polygon or circle obstacles? So the NavMeshModifier, if I put that on non-tilemap object, can I define a shape on that object to apply the non-walkable property too, if that makes sense?
can you bake this to game objects or does it have to be tiles??
this is a very useful video dude
Thank you!!! You are another person to put into "Special Thanks" in credits of my game if i ever gonna release it 😭
Nice video! I just found difficult the Tile Palette since I've never used it before and it was covered with yourself, but great video overall!
dam this helped thx man!
Hey! Great video! I'm having trouble setting up sprites etc. (Around 4:15) Is there a video you can recommend? I am quite new to Unity but I have coursework due in January 😅
Amazing vid thank you so much
When I hit bake, my map did not highlight at all. I followed the steps exactly but still didn't get any indication that it worked.
@chookki
2 күн бұрын
mine too. I even followed the READ Me, same thing.
what do i do to put an animation on all directions when the enemy moves? i have all animations created in a blend tree
@TheDynamicDudes
3 жыл бұрын
did u ever figure out how to do this?
thank you it take me a lot of time !!
how would you make the player rotate like navmesh?
Do you need a "Walkable Ground" in order for the agent to move on it? I basically have a maze (made of tiles) and that will be "Not Walkable" but do not have anything within the maze, the agent should be able to walk within the walls of the maze, right?
@RubyInferno
3 жыл бұрын
Update: I made it work, boy was that a mess or what...I had my Tilemaps all messed up, it got super tricky because I pasted my version of "Wall tiles" on the "Space (my version of "Ground")" tilemap...which really confused the heck out of me since the monster kept going right through it. I am building PacMan from scratch & now I have managed to get it working - the monster traverses in every nook and cranny like a boss! There was another really "epic" problem....Since its PacMan, my maze needs to only allow linear movement to the monsters & the gap between maze walls was too less for the algorithm to detect movable paths...until I discovered the nifty attribute inside the "Advanced" settings of the NavMeshModifier which allows you to change voxel size and make it super accurate at the cost of higher processing power - boy did that work like a charm or what! Blinky now traverses even the smallest gaps with ease, thank you so much for this! :D Sorry for the long post. xD
Is there a way to simply get the direction vector the pathfinding thinks the object should move in so you can manipulate it for more specific behaviors (like implementing pushing from other objects, changing speed, etc.)
@TheZBillDyl
3 жыл бұрын
You should be able to just add a collider to the agent and the agents should no longer phase into each other. As for specific behaviors, im sure theres a way. I think you can access specific nodes in the path that it calculated so you could calculate the direction vector from that
Is there a way to check to see when the targeted destination is accutlly meet? So that the target can change or a set of code runs when they get to their destination?
@TheZBillDyl
Жыл бұрын
Yes of course! Vector3.Distance is a function you can use to check when the target reaches its destination. Just use a threshold variable and bam. You got your functionality!
very helpful, thank you
This a great video, you are making a great service TO THE WORLD.
@TheZBillDyl
3 жыл бұрын
My only complaint is the beginning of the video has such poor video quality :/
Nice one dude
thank you so much man , i will be really helpfull
@roboticol6280
3 жыл бұрын
"i will"?
Thanks man!
Thanks for the video, i was trying to use A* for a while, and it was giving me problems with every single change i made
For anyone else who might have the problem I did, where you can't see the NavMesh after you bake it, in the 'Scene' window, in the bottom right change 'TileMap' to 'Focus On' Grid. But it's good tutorial, thanks!
@katal_do301
3 жыл бұрын
There's nothing in the bottom right part of the scene window.
@sulkingcrow7426
3 жыл бұрын
@@katal_do301 You can get the window by opening a Tile Palette window (Window > 2D > Tile Palette) and then the 'Tilemap Focus Filter' window should open bottom right of your scene
@katal_do301
3 жыл бұрын
@@sulkingcrow7426 Thanks a lot, have a good day.
How do you attach the agent to a 2D sprite via code instead of the editor? I have a 2D game object I'd like the agent be attached to. Thanks a lot!
@TheZBillDyl
3 жыл бұрын
Should be able to utilize the “AddComponent” method? Perhaps?
@RubyInferno
3 жыл бұрын
@@TheZBillDyl Yes I tried that but I am not sure where the "name" of the particular agent goes, for example I declare an agent: NavMeshAgent SomeAgent; Attach it to the object. SomeObject.AddComponent(); How do I give the name? (I tried putting it as a parameter in the brackets after but that doesn't work. Thanks!
I don't know if I will get a reply to this but I want a prefab of an enemy AI to pathfind to my character? But when I make the ai a prefab its says that it can not convert transform to a game object. Is there any way to do this that allows the ai to target the player with somehting like following the player's tag?
@TheZBillDyl
2 жыл бұрын
Yes! I think your issue is something to do with the way ur instantiating the prefab. If I were to make a prefab enemy, Id make it so a Game Manager (or an AI Spawner) spawns the Ai. The AI, in its own movement script, finds the player by its tag and gets it's transform. That transform is then made as the target! (Thats a rough idea of how id do it)
Absolute life saver. You desver more likes and views than this. Thank you SO much.
Interesting, what about other features like having multiple objects avoiding each other as they move?
THANK YOU A LOT!!!😆😆😆
Great video but how to make rotate to move direction ? Sorry for my bad english.
Thanks for the tutorial! FYI, you can even get the reference of the NavMeshSurface2d object and invoke BuildNavMesh on it to trigger the baking via code. Might help for dynamic maps?
@RubyInferno
3 жыл бұрын
Mind sharing the lines of code for baking via scripts? That would br amazing!
@hrishikeshgarud7177
2 жыл бұрын
@@RubyInferno public NavMeshSurface2d surface2D; surface2D.BuildNavMesh();
If you are looking for a way to implement this on a 2d game object and cannot see navmesh baked area then place your gameobject as a child of navmesh gameobject and select children in collect objects of navmesh surface 2d it will bake properly.
At first it didn't really work well for me, and so I rescaled my grid to 1,1,1 and now it works fine!
Thanks man =D
Thanks for this!! it totally solved my issue! i could make the player move on mouse click before, but it wasnt really recognising the NavMesh and just going through the walls :/ Just a quick question: why exactly did we make an empty game object at the end to feed into the target value?
@TheZBillDyl
2 жыл бұрын
To give the agent a place to go! :D
i cant find NavMeshCollecSources2d script in github file, how to find this?
Can the wall update during run time by using this method? Anyway thanks for this video.
I've been having an issue, I wonder if anyone else has encountered it. When I enable the Nav Mesh Agent on a game object it's sprite becomes invisible in the game view, but not in the scene view. If I turn the Nav Mesh Agent off it becomes visible in-game again. Any idea what's causing this? I've tried messing with the Sprite Renderer sorting layers but I'm stumped
@justinlindenthaler2491
3 жыл бұрын
having the same issue on my end!
@GenerallyBananas
3 жыл бұрын
@@justinlindenthaler2491 unfortunately I never found a fix, but it must be caused by some setting somewhere because I started a new project and haven't had the issue since
@JoaoGoncalves-np6su
3 жыл бұрын
@@GenerallyBananas if you're making a 2D game make sure that the z component on the transform is not too far away from the z component of the active camera, that sometimes happens to me and the sprites become invisible
@yotonking2831
2 жыл бұрын
It's because you put -90 in the navmesh transform. Your sprite also changes rotation to -90 and makes it seem invisible. Remember to change agent.updaterotation = false
@GenerallyBananas
2 жыл бұрын
@@yotonking2831 Ooooh! I think that's it!!! I ultimately fixed it by just redoing from scratch, but this is great to know for future reference, thank you!
Sorry to everyone who gets this a lot, but is there a way to decrease the size of the margins between the baked navmesh and the walls? It sort of seems like Override Voxel Size is the place to do this but it's not really clear how it works.
@TheZBillDyl
2 жыл бұрын
I think it’s the size of the agents? I think? Sorry it’s been a long time. Good luck though!
Thanks a lot
Hello, hey when I put a "prefap" of the enemy, a radius is created that makes the enemies are at a specific distance, how can I solve it?
@TheZBillDyl
3 жыл бұрын
Im not sure im understanding the question? Do you mean a stopping distance?
@richg8097
3 жыл бұрын
@@TheZBillDyl It seems to me that it is something of the radius in which the enemy can approach the player
@TheZBillDyl
3 жыл бұрын
Im still a bit unsure of what you’re asking. What are you trying to solve?
@richg8097
3 жыл бұрын
@@TheZBillDyl Don't worry, I already solved it
Did anyone get this to work with prefabs? When my Enemy Spawner creates a new enemy, they just kind of sit there and don't pursue the player, although the initial game object one does. It did work in my non navmesh version of code.
@Simo-un2zu
Жыл бұрын
Nvm, this worked in void start: target = GameObject.FindGameObjectWithTag("Player").transform; with Player being the tag set on my player object. Now my enemy spawner makes the enemies at certain intervals, and they follow the player using the navmesh AI. :)
The tutorial wont work, I mean i know its been 2 years but is there another way to do this with changes they made
Oh brother, been fighting with this path finding for a while already. Giving me error "showNavigation is no longer supported and will be removed' :(
I'm wondering if using tilemap is necessary or can I just use sprites
@TheZBillDyl
Жыл бұрын
I've gotten this question a lot. Honestly, I have no idea. This is an older video so i dont remember everything. you could place your image into the tilemap as a tile then increase the size of the tile on the tile grid. That should work?
Hey man, thanks for the video. How would I get this to work with a randomly generated 2D maze? I need to find a way to bake my nav mesh at runtime but cannot for the life of me find out how to do it.
@TheZBillDyl
2 жыл бұрын
Sorry for being a year late but, there IS runtime baking available. Check out the GitHub link in the description and near the end of the documentation there’s a section on runtime baking.
@Shringyshrong
2 жыл бұрын
@@TheZBillDyl hey man, thanks for the reply any how! I actually managed to figure it out by implementing my own A* path finding algorithm that would constantly update at run time :) really good to know that there is runtime baking tho!
@darkeye4501
2 ай бұрын
@@TheZBillDyl I'm digging up the thread after another 2 years. Yes, there is a section in the documentation on how to trigger navmesh generation in runtime, but I don't seem to understand how to do it and I urgently need an explanation.
Intro was great lol
Thank you so muchh
U r just creating one empty game object and move it manually and enemy is just following the player.. what if i want the player like i will touch and the player will move where i touch how to do it on nevmesh?
@TheZBillDyl
3 жыл бұрын
You would just move the position of the empty game object (the target) to where the player touched!
Thanks!!
is there anyway to make them move around the map randomly, until the player is a certain distance away?
@TheZBillDyl
Жыл бұрын
Of course! Simply move a target transform to a random location. It can be invisible. This is somewhat how most games do it tbh. (Granted a bit more complex)
@Dbap1997
Жыл бұрын
@@TheZBillDyl am i able to have multiple nav mesh in one scene ?
Does this work without tilemaps? So with separate objects with sprites and 2d colliders?
@neobichniymarketplace
Жыл бұрын
Good afternoon, did you find out the answer?
What software do you use for that awesome windows desktop background?
@TheZBillDyl
2 жыл бұрын
Wallpaper Engine
it doesnot work for me. navmesh tells me that i need atleast one tilemap... which i definitely have more than one.
THE GREATEST:DDD
@tripw7736
3 жыл бұрын
BRO DO U HAVE A TIPJAR???? I WILL GIVE YOU MONEY FOR THIS YOU MARVELOUS HUMAN:DDDDDD
@TheZBillDyl
3 жыл бұрын
Just sub to me and tht counts imo haha. You’re welcome!!!
@tripw7736
3 жыл бұрын
@@TheZBillDyl DONE BRO🤩🤜🤛💪💪💪💪
I started with unity 3 years ago. Then I tried to make own assets in Blender and fell in love with 3D modelling and Pixel art - let's say I never touched my game again but I have assets for years now :'(
@TheZBillDyl
3 жыл бұрын
Yea. A good thing to get in the habit of is understanding the scale of a project youre capable of doing!
@FonzieKree
3 жыл бұрын
@@TheZBillDyl more like I kept learning blender until I became 3D modeller instead of indie dev - my original purpose. No regarrets tho! I enjoy drawing in Blender and Aseprite
Navmesh surface 2d and navmesh agent haven't exit anymore
VC works what you are doing in the inspector with video above
Can you use this for free in any game without legal issues? Like if i sell a game
@TheZBillDyl
Жыл бұрын
I believe so. You would have to check the license on github
Can I bake the navmesh in the runtime ?
@TheZBillDyl
3 жыл бұрын
U should be able to? I think theres a method to call on it someplace
thanks
Ok... This work for tilemaps, but, How to make it work for independent game objects?
@TheZBillDyl
3 жыл бұрын
I'm unsure but, if you're doing 2D you more than likely should be using tile palettes. Can you explain what you're doing? I can try to help!
@jonathanbarone1257
3 жыл бұрын
@@TheZBillDyl Of Course! I am trying to generate a navmesh around the prefabs like trees, etc, to make npc pathfinding. I still can't do it
Thanks i need pathfinding for my bots in my battle royale Thanks so muck bro
pog
astar pathfinding was not working on 2020.2.. so thanks !
can u tell me how i can move it by mouse ckick
@TheZBillDyl
3 жыл бұрын
Ill give you a hint. Utilize the function Camera.main.ScreenToWorldPoint(Input.mousePosition);
I'm assuming this doesn't work with 2D rigidbodies?
@TheZBillDyl
Жыл бұрын
Ummm it should? Just make sure the gravity is turned off? I am 95% sure this would work.
thanks, but man change your webcam pos plz :D
i cant find NavMeshSurface2d component(5:14) ((((((((((
@user-yx5bf7dp4s
8 ай бұрын
It has changed to Navigation Surface.
I can't get navmeshsurface component
@NoShell
Жыл бұрын
Some things changed in the git Project just look at the this is the official description now.
@redoctober2088
Жыл бұрын
@@NoShell yes figured it out, but thanks anyway
can it avoid moving objects?
@TheZBillDyl
3 жыл бұрын
Should be able to?
@gustavosalmeron2013
3 жыл бұрын
@@TheZBillDyl Yes, I am programming a 2D top down game with moving obstacles. I am using A* with a grid, but SOME objects aren't the exact size of a tile.If a character gets inside the unwalkable area, he doesn't know what to do. I need a system where there are multiple characters with rigidbody2D colliders walking around and they can see in real time where the objects are moving, to avoid for example, 2 enemies trying to walk the same route, but in opposite ways, and get stuck.
hello, can we make games with this plugin and earn commercial income?
@TheZBillDyl
2 жыл бұрын
Hello, The plug-in uses Unity’s built-in navmesh but, slightly modifies it. With that said, I double checked the GitHub page and there’s no mention of a license so I believe it’s open source and free to use because it’s still using Unity’s technology! So in other words: you’re good to go!
why does it turn so slowly
Thanks for the tip. Are you in the Navy? The blue shirt, haircut, the beds, and the walls makes me assume so. Either way, good on you learning game dev.
@TheZBillDyl
2 жыл бұрын
Haha! No, in this video I was in my college dorm! ❤️ Don’t worry most ppl think I’m in prison so I’ll take this view any day 😂
@grant3044
2 жыл бұрын
@@TheZBillDyl Haha XD , well aren't I sheepish for assuming; jk :P . (I should've known, the corners of the sheets weren't folded to regulation standards haha) Well you'd make a fine sailor either way. Again, double thanks for the help. Goodluck with your gamedev.
What can I say. I like Knobs.
Nice tutorial, I like it! There are some updates so following this wont work 100%, I've created a new tutorial on my channel to follow along 2023 for this.
Please set your web cam view somewhere else, not in the inspector
@TheZBillDyl
Жыл бұрын
I totally agree. Thank you for the heads up
helal lan (turkish god bless you)
This is pretty good stuff from a guy in jail
@TheZBillDyl
2 жыл бұрын
its my college dorm....... but ur right its basically jail
my enemy hovers
where did you get that wallpaper? its so cool :o
@TheZBillDyl
3 жыл бұрын
I got it from Wallpaper Engine!! Its $5 on steam but, it’s super cool!
omg bro im actually shocked how well this is working with barely any work! the only problem i had was that they changed some of the component names but the github talks about it
@gamecube28
7 ай бұрын
What is the nav mesh agent called instead