2024 AI Pathfinding: Unity 2D Pathfinding with NavMesh tutorial in 5 minutes
HTML-код
- Опубликовано: 20 июл 2023
- GitHub project for 2D Navmesh pathfinding:
github.com/h8man/NavMeshPlus
If you want to know how to bake at runtime, you can read about it here:
github.com/h8man/NavMeshPlus/...
Join the discord server: / discord
For anyone having trouble getting it to bake your different zones, double check your sorting layers and z position on the transform. make sure z is 0 for all objects
This comment should be pinned!
Lifesaver
By far the best tutorial to this topic. There is no offtopic, no hyperactive and stressfull voice, just getting straight to the point and showing what to do
Thank you! Yeah I felt that this was missing when I was looking for a tutorial on the subject :)
If you want to make the navigation mesh more dense, add "Window -> AI -> Navigation", adjust "Radius" and try Bake again.
this is so much better than A* thank you for this
Great tutorial man, you explained so well it was really easy for me to transfer the technique to a 2d tilemap.
Works perfectly, thank you so much :)
my pleasure
If I set updateUpAxis to false, then the agent can't move upward in my scene view. But if I comment the line out, the rotation is set to -90 on the X axis (which renders the sprite invisible on the screen). For now I have the update function setting the rotation to 0/0/0, but then the agent is constantly moving on the Y axis (albeit very slowly). Yes, I have updateRotation set to false.
I couldn't help but obey the orders of your commanding voice. Thank you!!
😂 Thank you, I'm just trying to help!
What a true GOLD tutorial thank you a dozen times
Thank you!
No BS. Pure goodness. Thank you sir.
Glad I could help!
Thanks for this!!
Dude this is a great video. Just earned a new sub!
Thank you very much!
Very nice Tutorial. Easy, fast and clean.
Thank you very much!
Just tested it on 2022.3.19f1 and still works. I was straggling for a while to make it bake properly. I was able to fix it by adjusting navigation options - especially the radius. I think my map was to small to work with default values set by default for "Humanoid" agent type. Anyway, thank you for the video!
Thanks for the info!
Incredible tutorial, super helpful and easy to implement. I'm trying to allow the enemy to rotate while navigating. Whenever I try and make it rotate I made the updateRotation true, but then it allows rotation on all axis, only am looking for Z-Axis. Is there any way I can limit that rotation to the Z-axis only? Thanks again.
Great video, thanks!
Thank you for watching!
Could this be implemented in a procedural tile-based level generation? Could the scripts be somehow applied to - for example - "Walkable" and "Obstacle" tagged tiles after they are generated?
It might be covered here
github.com/h8man/NavMeshPlus/wiki/HOW-TO#bake-at-runtime
thanks for video, works perfect for me
Im Glad you liked it!
thank you very much i tried to make it by myself but it was to hard, fast clean and perfect !
Great Tutorial !!!!
Is it possible to rotate in z axis to know the forward direction ? I wanted to add an animated movement.
ths video is great and there are so little like this - thanx!!! i have small question -i am looking to fcreate the enemy going after the player but still stics to the walls - like a spider runing on ceiling floor and 90 degrees walls. but this video shoes the best and shortest route so he doesnt stick to the "floor in 2d like he stick to the floor when its in 3d navmesh - any way to make it stick to walls and floor?
Great video!
Had troubles with getting a Companion AI visible when running the game, but attaching the component: Agent Override 2d fixed the issue.
Awesome!
I didn’t even know 2D pathfinding was a thing 🤯🤯
It sure is! :D
That's why your basics should be clear.
thank you
Thanks for the video!
Is there a way to change the movement speed as I desire for each enemy?
My pleasure! You can change the "Speed" variable in the NavMeshAgent component
Hi, I have this error, any ideas ?
"SetDestination" can only be called on an active agent that has been placed on a NavMesh.
Amazing tutorial! But I get a GUID conflict when i installed the AI navmesh from the package manager, what should i do?
Restart Unity 😄can you show me the error message?
Thank you, it was:
GUID [455cd750f394b1c41b963b3335eae29c] for asset 'Packages/com.unity.ai.navigation/Tests/Runtime/Unity.AI.Navigation.Tests.asmdef' conflicts with:
'Assets/NavMeshComponents-master/Assets/Tests/NavMeshComponentsTestsPlaymode.asmdef' (current owner)
We can't assign a new GUID because the asset is in an immutable folder. The asset will be ignored.
I also get the warning:
You are trying to import an asset which contains a global game manager. This is not allowed.
when I put the NavMeshComponents-maste into Assets, I also get the arning:
You are trying to import an asset which contains a global game manager. This is not allowed.
@@rootbindev
for some reason when i click bake, the navmesh is not visible on the scene view
i m using unity version 2021
EDIT: I had gizmos visibility toggled off lol
Had the same issue. You're a lifesaver :)
Thanks
Having an error: "SetDestination" can only be called on an active agent that has been placed on a NavMesh. Do you have any ideas why my agent doesn't get registered on a navmesh.
UPD: Solved, the problem that my Nav Mesh Agent height was too low.
Thank you!
Followed your tutorial but I don't know why I can't drag the player into the target input on the enemy object. Any recommendations? Thanks in advance
Make sure you have the Enemy script attached to the Enemy object before dragging :)
@@rootbindev Thanks for this quick reply but I think I had other issue. My enemy is a prefab but my reference object was not a prefab. Fixed it tho :D
@@kanoblack6179 im having the same problem. how did you fix it?
@@MrEthan. If one of the objects with the script attached is a prefab, both has to be prefabs
Dude , ur godlike
Thank you very much!
What about Navigation Modifier Volumes? They seem to get ignored by the baking process.
Waiting for next video
What would you like to see?
overlaping walkable sprite on top of obstacle is not considered, in my case i have a ground and a big empty room, upon bake 2 navmashs created outer and inner, but when i overlap a walkable sprite on a part of wall it just dosnt considered walkable. can anyone show me how to?
Thank you so much
Quick question. Navmesh Agents will over ride colliders that were not baked. They will literally walk through walls when they have a destination.
how do I fix this problem?
Navmesh repositions your objects in a static fashion. Use velocity to reposition your characters and it will respect the colliders.
Hey, when adding the components to my project I get 21 warnings like this:
"Assets/NavMeshComponents/Editor/NavMeshSurfaceEditor.cs(80,13): warning CS0618: 'NavMeshVisualizationSettings.showNavigation' is obsolete: 'showNavigation is no longer supported and will be removed.'"
Is this package still good to use, or do I need to resort to unitys own new AI navigation package?
Any help would be appreciated, thank you.
Nevermind, I tried to include my own naming conventions already and messed something up along the way. It works now, thanks again! :D
Hey, I was wondering if it was possible to bake the navmesh during runtime?
I'm having trouble finding the solution online.
Hello! Take a look here, search for "Bake at runtime" github.com/h8man/NavMeshPlus/wiki/HOW-TO
@@rootbindev Thank you!
should be also pinned 100%. and thx ♥
I will add it to the description :) @@user-ks8vn6zn3w
Hey, does this work for movable obstacles?
Hello, I'm not sure, I haven't tried it. I guess we would have to rebake alot for every move
Urgent question. How do I bake the navigation Surface from the code? I've got a dynamic map and I cant figure it out
[SerializeField] private NavMeshSurface navMeshSurface;
private void Awake() {
navMeshSurface = GetComponent();
}
//and call this function when u need to bake it
private void BakeNavMesh() {
// Build the NavMesh
navMeshSurface.BuildNavMesh();
}
i cant drag and drop the nav mesh components in the assets for some reason
nvm figured it out
my game objects disappear when i add the nav mesh agent component to them. i changed their order in layer but they still wont appear
Try checking your gameobjects Z position value, is that always set to zero when you play?
for some reason my gameObjects z position always goes to -1.169839, any idea why?
Have you tried this :
void Start()
{
navMeshAgent = GetComponent();
navMeshAgent.updateRotation = false;
navMeshAgent.updateUpAxis=false;
}
why does the compiler not enter _agent.SetDestination(hit.point); ?
Is it possible to make multiple enemies come to the player and stand around the player, the enemies will avoid each other like they avoid obstacles?
Have you tried giving them the "Navigation modifier" component and set the override area to not walkable?
My obstacle doesn't bake properly despite having it set to not walkable, any ideas why?
Hmm, hard to say without more information. Did you follow the tutorial?
@@rootbindev I did it all again and now it works, thank you
I'm glad to hear that it works! @@wakizashidev9481
Suppose you have a grid (it could be a TileMap2D) where the rule is that crossing diagonally has a cost of "3" and sides "2", plus your enemy has a maximum movement of "10". How to do it with NAvMeshPlus?
hmmm didn't work for me says that "Failed to create agent because it is not close enough to the NavMesh"
What how do i fix this.
Please help, if I create a copy of an enemy, they appear in the corner of the map and do not move
I guess you set the enemies position somewhere
No, because if there is only one enemy everything works@@rootbindev
@@Rayreallife Open your wallet and pay him if you want him to solve your problems. You think everyone is here to work for free at your demand? ROFLMAO, kids...
I cant find the nav mesh agent in the add component field
Did you follow from 4:13
Yes i did everything But. I Fixed it, problem was unknown i just did nothing for 5 min and then it popped up
Is there a way to use this method, but still moving enemy with Rigidbody?
As far as I can find, no. I'm having the exact same issue. Originally I was going to use Unity's calculatepath method and just AddForce into the direction of the next path, however, navmeshplus does not seem compatible with that. The only other option left I've not tried is finding a way to use the agent with updateposition set to false, and seeing if that path works.
This is a great tutorial, but when i arrive to the step of adding the ai navigation packege it wont appear.
(if someone know how to solve it please tell me) :'(
I think you are using too old version of unity
ok i'll check@@rootbindev
Thanks )))
how can i make it move faster? ty!
When you play, the z-value of the enemy's transform position changes even though it is 2D. This is also visible in the video.
Oh, I haven't noticed! Nice that you found it and told me about it
When i add nav surface it says Object reference not set to an instance of an object
Try restarting unity
How do I do this with multiple NPCs? I want a lot of them moving with pathfinder
duplicate it! :)
help, i cant reference navigation surface to bake in the code, thank you
Did you follow the tutorial? :) What happens when you press Bake?
I added the navmesh surface to my level manager "public NavMeshSurface surface;" then I can call "surface.BuildNavMesh();" to build at the start of each level or if I shift the level mid gameplay. that basically runs the bake function. Note you need to include "using NavMeshPlus.Components;"
Does this work in Unity 2020?
as far as I know it should, but I'm not 100% sure
doesn't work in 2D. help, what am I doing wrong? I tried to repeat the tutorial in different versions, the grid is not being built
I don't know what you are doing wrong, it works for me as you can see in the tutorial :) Maybe join the discord and show what you are doing and I might be able to help
AI navigation is not present in package manager😢😢😢help me
its not working .
i use click to to move and when i click to move at random position then player is passing through the obtacles, even though obstacles are not walkable and in the scene they are also not blue .
I'm sorry to hear that
@@rootbindev how can i move my player ?
thats very hard to say since there is so many different ways you could have developer your game :) maybe if you show me your code in the discord channel?@@MMonis-ge6lh
i love you
❤
@@rootbindev Any chance you would know how to bake at runtime as well?
@@flyteful I might make a video about it :) until then you can follow this link github.com/h8man/NavMeshPlus/wiki/HOW-TO#bake-at-runtime
Hi. I've been struggling with navmesh for months. I have watched this video a dozen times. I cannot get it to work. I add the navigation surface and the navigation collect source2d. click rotate surface to XY and unlike you, after i press bake. nothing happens
gizmos are on
Hi!
Do you use the same version of unity as I do in this tutorial? 2022.2.8f1.
Did you make a 2D project?
It might be easier to help if you join the discord server
i tried but the link is broken. @@rootbindev
project is 2d and I'm using 2022.3.10f1. @@rootbindev
okay what if I wanted the enemy to run away from the player instead
figured out a kinda scuffed solution of just having an object rotate around the enemy based on where the player is and making the enemy follow it
Maybe you could set the target to something else then :)
Hmmm.. I'm not sure about performance and combability of this. Unity should add simple 2D Nav system.
I Agree
There is no ai navigation in my package manager
Did you select Unity Registry in the dropdown?
@@rootbindev I did, I think it;s because I;m using 2020
but I can still use it with git
can someone help me with the script? It doesnt work at all and my enemy just dissappears.
show me in discord and I might be able to help
Can this work for platformers
Yes this works for platformers, it depends on what you want to get out of it :)
01:19 !!!!!!!!!!!!!!!!
?
@@rootbindev very important timestamp. People shouldn't skim over the video
:D That's a very good advice@@sayochikun3288
Stop yelling!
🗣
@@rootbindev 😭 I didn't do anything wrong! Stop yelling at me!