h8man / navmeshplus Goto Github PK
View Code? Open in Web Editor NEWUnity NavMesh 2D Pathfinding
License: MIT License
Unity NavMesh 2D Pathfinding
License: MIT License
Hello, iam using this and there is no "Grid" Option (Collect Objects) like the Tutorial say.
Iam using a Tilemap2DCollider and it not working. I followed the tutorial
Hey! I've setup your NavMeshPlus, but I have problem as you can see on screen:
Mesh is really inaccurate. I have 3 tilemaps. Ground, OnGround and Objects. Objects are marked as not walkable. I didn't make any extra steps. Object tilemap has tilemap collider with composite collider.
Could you help me? What can be reason?
In an effort to try and stop agents bumping into each other and getting stuck, i've tried lowering the obstacle avoidance radius - but anything below 0.3 stops pathfinding from occuring.
My end goal is to get agents being able to use the same space for navigation - but feel like i've come unstuck trying to get this working. To smaller avoidance radiance and the bottom seems to fall out on 2d pathfinding...
l was wondering if this had something to do with where the agent sits vs. where the nav mesh is, and the agent is perhaps not considered 'on the navmesh' if he has too smaller radius...
This line throws when using a NavSurface2d set to CollectObjects2d.All
without a Grid
object. A Grid
is not required when using colliders. It seems as though CollectOptions.Children
is the only option compatible.
Also, if you do add a grid at the root of colliders, it complains "NullReferenceException: Add at least one tilemap", rather than simply scanning its children for colliders.
I think it should be pretty straight forward to either 1. scan the whole scene for geometry, or 2. fix these errors by preventing invalid configurations in the inspector.
This was useful, but I think there should be an option to disable trace logs as they're messing up my console. Would accept a PR?
Hi,
Sorry about this, I am getting a weird issue with the asset.
I have a tilemap world, I have baked the navmesh and it's baked nicely, and the AI do move.
However the AI tend to get stuck every now and then and won't move, sometimes they start moving once the player moves or if I just leave it long enough, but I have no idea why this happening.
I have attached some images of:
How the baked navmesh looks
What seems to be occurring
Will also include the code of my AI:
AI can wander around randomly
AI can patrol by moving towards patrol points.
Thanks
Above Gif is not representative of the framerate in-game, everything runs smoothly in-game except for the movement
private IEnumerator PatrolAround()
{
while (patrol)
{
if (patrolPoints.Count > 0)
{
agent.SetDestination(patrolPoints[patrolIndex].position);
//if (transform.position == patrolPoints[patrolIndex].position || Vector3.Distance(transform.position, patrolPoints[patrolIndex].position) < 0.142f)
//{
// //Debug.Log(Vector3.Distance(transform.position, patrolPoints[patrolIndex].position));
// patrolIndex++;
//}
yield return new WaitUntil(() => Vector3.Distance(transform.position, patrolPoints[patrolIndex].position) < 0.142f);
patrolIndex++;
Debug.Log("Made It");
if (patrolIndex >= patrolPoints.Count)
{
patrolIndex = 0;
}
yield return new WaitForSeconds(1f);
}
}
}
public IEnumerator WanderAI()
{
while (wanderAround)
{
Vector3 newPos = RandomNavSphere(transform.position, maxDist, -1);
agent.SetDestination(newPos);
if (Vector3.Distance(transform.position, newPos) != Mathf.Infinity) //sometimes the distance is infinity - not sure why
{
yield return new WaitUntil(() => Vector3.Distance(transform.position, newPos) < 0.142f);
}
yield return new WaitForSeconds(1f);
//Debug.Log("Woo");
}
}
public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
{
Vector3 ranDir = UnityEngine.Random.insideUnitSphere * dist; //finds a random vector in a sphere distance
ranDir += origin; //adds current pos to that pos
NavMeshHit navhit;
NavMesh.SamplePosition(ranDir, out navhit, dist, layermask); //finds the closest point on navmesh within range, layermask will be -1 as that means all layers
return navhit.position; //return the random position
}
```
Encountered this trying to add WASD type movement - agent will only move if velocity.x has a non-zero value.
Using this code:
` var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
if (horizontal != 0 || vertical != 0)
{
var direction = new Vector2(horizontal, vertical);
agent.ResetPath();
agent.velocity = direction * agent.speed;
}`
Pressing up&right together works, just right/left works, just up/down fails.
Hello
My stages are saved in prefabs.
I could bake the mesh correctly in prefab mode but it won't show the result unless I am viewing the mesh from a normal scene with the prefab. This issue make it a little hard to evaluate mesh result right after baking. It would be very appreciated if this can be improved.
Thanks for the excellent work.
Steps to reproduce:
Issues:
The issue does not appear when the swizzle & tilemap orientation settings are left at their defaults. (see image below)
Specs:
Hey again @h8man!
Running into another issue and probably doing something wrong, but wanted to see if you've seen this scenario:
We've been randomly generating our tilemap and then programmatically calling the NavMeshSurface2d.BuildNavMesh()
using a custom GUI Editor button on our component that generates the level and builds the NavMesh. We had some issues at first, but as you may recall in #41 we were able to resolve them.
Now that we're happy with our random level generation, we'd like to do it at the start of the scene when playing. Our level generation works and the BuildNavMesh()
sort of works. It builds the nav mesh, but it doesn't use the same NavMesh data file, and for some reason though the NavMesh that is applied is the one that was last generated programmatically via our custom GUI Button.
I suppose the important thing to keep in mind is that whether or not you press the our GUI button to generate the level or start the scene to generate the level, the same exact code is being called.
Here are some screenshots - hoping you my have some suggestions on where to start investigating:
Pressing our Custom GUI Generate Level button works as intended:
After pressing Play a new level generates, but it using the previous generated NavMesh (notice the Nav Mesh Data is just called NavMesh
now though):
After stopping the scene, our tilemap will revert to the last level we had generated outside of play mode (this is normal) and of course it's Nav Mesh Data is back to NavMesh-NavMesh
Hello.
I have been using NavMeshPlus in the development of a Roguelike/RPG with pseudo-random, procedurally generated areas. Each area is composed of multiple "rooms", each with different elements and tilemaps. These rooms are made using the Prefab system in Unity, with specific components (such as containers or enemies inside the room) being randomly generated afterwards.
The problem is as follows: in order to use the Prefab Editor in Unity (which is a necessity to comfortably modify the room prefabs), the tilemaps need to have a parented Grid, or they aren't properly displayed. The easiest solution to this problem is to have a grid in each room prefab. After quite a bit of work with the prefabs, I found out that the NavMeshSurface2d didn't generate the correct surface: it ignored every other grid in the scene except the first one it found, and so it generated only the NavMesh for one room.
I took a look into the tool, and saw that in the lookup for grids in the scene (GetRoot function in NavMeshBuilder2d), even though an array of GameObjects is being returned, it will always contain only the first grid it finds:
switch (CollectObjects)
{
case CollectObjects2d.Children: return new[] { parent };
case CollectObjects2d.Volume:
case CollectObjects2d.All:
default:
return new[] { GameObject.FindObjectOfType<Grid>().gameObject }; // Only returns one grid
}
For my particular problem, I fixed it by changing that last line as follows:
default:
List<GameObject> grids = new List<GameObject>();
foreach(Grid g in GameObject.FindObjectsOfType<Grid>()) {
grids.Add(g.gameObject);
}
return grids.ToArray(); // Returns all gameObjects with a Grid
I tested it with several rooms (all of them relatively large in size) and the surface looked like it was being created properly.
There probably are a few other solutions to my specific problem, but this works for now. Was there any reason for the original line?
I took a look at the wiki and pdf and it seems that it only works as a topdown game, is there a way to make it work with a 2d platformer?
Some scripts in NavMeshComponents\Scripts
are referencing missing icons, which seems to be in https://github.com/h8man/RedHotSweetPepper/tree/master/Assets/Gizmos .
I'm developing some 2D isometric world generation logic using NavMeshSurface2d
and NavMeshModifer
scripts. So far, all the tile colliders have worked ok when baked with the script (calling NavMeshSurface2d.BuildNavMesh
).
However, when adding a GameObject to the mix which has it's own polygon collider, the script call to bake the mesh does not include the gameobjects' colliders. When I generate the world, then press bake from the UI the colliders are baked fine.
Is there a reason it would work from the GUI but not scripted? Could it be a timing issue? Do I need to 'construct' the colliders somehow?
I don't know why but NevMeshData property is disabled in the NavMeshSurface2D inspector. It would be great if I can click it and found it in project panel so that I can rename or even move them accordingly based on the stage I am editing. e.g. Either by making it enabled or simply add an inspector button to ping and select the object in the project panel.
Thanks for reading.
Hey,
so for some reason the agent doesnt want to avoid the non walkable areas, despite having a valid navmesh.
Basically what im doing is, setting a destination that is below the object on the Y axis. It starts moving but then gets stuck on the non walkable areas.
Gif: https://imgur.com/8fhpCmE
If i enable back rotation and upaxis it rotates properly around the object, but it messed up everything :D because the navmeshagent doesnt understand its on a 2d plane :)
Any clues how to solve this?
Good afternoon!
Firstly I just want to say thank you for this great tool!
I'm attempting to generate a bunch of boxes at run time, Before run time, if I place a Navmesh obstacle on the boxes and hit carve, the boxes carve out their area in real time, The same thing happens if I bake the navmesh.
But if I try to create the boxes at run time, Even if they have the Navmesh obstacle set to carve, the navmesh still generates over them. Is there something I need to do to get it to update with the generated boxes?
I'm 99% sure this is my lack of knowledge of the navmesh system in general.
Any help would be appreciated
Thanks!
Add flag "Tilemap Compress Bounds" to NavMeshSurface2d to perform Tilemap.CompressBounds before bake
Hello, first of all thanks for the script!
The issue I have is that I have a big sprite for a building that uses a Polygon Collider 2D
component because its collision does not exactly match its shape.
I tried adding a NavMeshModifier
component to the sprite, and setting it to override Not Walkable
, but it is not being cut from the mesh.
Sprite Renderer
, scale the sprite to be big, several tiles largePolygonCollider2D
, give it a shape so the player can walk on some part of the spriteNavMeshModifier
, set to override "Not Walkable".NavMeshSurface2d
, have the "Default Area" be Walkable
and set "Use Geometry" to Physics Colliders
NavMeshSurface2d
The PolygonCollider2D
bounds should be cut from the baked NavMesh
Nothing is cut from the NavMesh.
One workaround is to use several Nav Mesh Obstacle
s to approximate the collision shape, but I'd really prefer to use the more flexible Polygon Collider 2D
because of the complex shape and out of some concern for performance.
I think the issue arises because NavMeshPlus detects the NavMeshModifier
is attached to a sprite and tries to use the sprite vertices before checking for any colliders on the GameObject.
Thanks again for the script, please let me know if you need any more information.
I just upgraded my Unity install from 2018.2.16 to 2018.3.0 this morning and on opening my project I received the following warnings and error:
Assets/NavMeshPlus/NavMeshComponents/Scripts/NavMeshSurface.cs(371,30): warning CS0618: 'PrefabUtility.GetPrefabType(Object)' is obsolete: 'Use GetPrefabAssetType and GetPrefabInstanceStatus to get the full picture about Prefab types.'
Assets/NavMeshPlus/NavMeshComponents/Scripts/NavMeshSurface.cs(372,31): warning CS0618: 'PrefabType' is obsolete: 'PrefabType no longer tells everything about Prefab instance.'
Assets/NavMeshPlus/NavMeshComponents/Scripts/NavMeshSurface2d.cs(465,30): warning CS0618: 'PrefabUtility.GetPrefabType(Object)' is obsolete: 'Use GetPrefabAssetType and GetPrefabInstanceStatus to get the full picture about Prefab types.'
Assets/NavMeshPlus/NavMeshComponents/Scripts/NavMeshSurface2d.cs(466,31): warning CS0618: 'PrefabType' is obsolete: 'PrefabType no longer tells everything about Prefab instance.'
Assets/NavMeshPlus/NavMeshComponents/Scripts/NavMeshSurface2d.cs(470,26): error CS0039: Cannot convert type 'UnityEngine.AI.NavMeshSurface2d' to 'UnityEngine.AI.NavMeshSurface' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
When using this and everything is going correctly, it does not include the one wide gaps between walls and i couldn't find any documentation on this. Is this intended or is there a spacing setting i missed?
Using unity version 2019.2.5f1 and NavMeshPlus version 0.1.0
I'm an idiot and didn't look in the right place. Sorry for being noob! Cheers!
Hello!
I am new to game development and am working on a top-down 2D adventure game in Unity. I am attempting to use NavMeshPlus for my enemy pathfinding, but I am running into an issue of disappearing sprites.
The issue arises when I add a NavMeshAgent component to my enemy game object. The sprite will be visible within the "Scene" view, but as soon as I move into the "Game" view the sprite disappears.
The NavMesh itself works well and seems to pick up on all of my collision tiles after baking, but I haven't been able to find any fixes for the disappearing sprites online. Hoping you'll be able to help. Thanks!
Is it currently possible to generate a navmesh at runtime, and if not, is this because it would be difficult to do?
Also, unrelated, how can I set the size of the scanned area? My game has no tilemap and there seems to be no way to directly set a grid's width and height. I've resorted to adding a grid and tilemap with corner tiles and its renderer disabled.
Thanks!
Hi,
Firstly thank you for this asset, it is a life saver and has helped me do a lot for my project.
Just wanted to ask, I did read your licence but wanted to clarify, is there no issues of using this for a commercial games project, would you like credits or anything?
Thanks!
Thanks for making this available publicly.
Following the repro steps in a 2d project on 18.2, upon pressing BAKE I get this error:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.AI.NavMeshSurface2d.CollectSources () (at Assets/NavMeshComponents/Scripts/NavMeshSurface2d.cs:290)
UnityEngine.AI.NavMeshSurface2d.UpdateNavMesh (UnityEngine.AI.NavMeshData data) (at Assets/NavMeshComponents/Scripts/NavMeshSurface2d.cs:174)
UnityEditor.AI.NavMeshSurfaceEditor2d.OnInspectorGUI () (at Assets/NavMeshComponents/Editor/NavMeshSurfaceEditor2d.cs:312)
UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor[] editors, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1367)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
I need to build a navmesh asyncronously at runtime. Although NavMeshBuilder
exposes no BuildNavMeshDataAsync
it's quite trivial to implement via this method.
I would like to add AsyncOperation NavMeshSurface2d.BuildNavMeshAsync()
for this purpose.
Would you be interested in a PR adding this feature?
https://docs.unity3d.com/Manual/Tilemap-Isometric-Palette.html
Add height override for Isometric Z as Y Tilemap
Hi, sorry if this question is silly (i am newbie in unity)
Is this mesh support dynamic obstacles?
I am using NavMeshPlus to guide a wizard enemy towards the player. It works great except when he teleports. To teleport, he sets his transform.position
to his destination. This causes the pathfinding to malfunction, though.
I am not sure if this is related but if the wizard collides with a Rigidbody2D
, he fixes himself. The Wizard also has a Rigidbody2D
and BoxCollider2D
.
I am using the Unity Isometric Tilemap example and trying to convert that to use your navmesh system. In the demo, they use Decorations which use physics colliders.
How do I go about doing this as I saw that you could do use geometry physics colliders, but can't get it to work?
Am I also not sure how I could get physics colliders and tilemap working alongside each other as the demo uses for building the world?
Looks like you completely ignoring Z coordinate of tiles.
I'm making a top-down 2D game. Despite it's in 2D, it's in Top-down Oblique projection, so it uses Z ordering for character to be able to stand behind a rock or behind a tree, for example. So, position of each tile is = Vector3Int( x, y, y ). But NavMeshBuilder2d.CollectTileSources() method iterates only through X and Y axes.
I managed to fix it by adding iteration over Z axis in CollectTileSources() method and setting Override Vector to (1, 1, 0). I don't understand what Override Vector is. It was just a guess. But it worked.
I hope you will add iteration over Z axis, so I can use your great tool without my own modifications.
Cheers
Hi, thanks for this great asset. I recently noticed a bug where NavMesh would be generated in places where tiles don't exist. Steps to replicate:
This is the result in my case (note that the green-isn area contains tiles beneath it and the blue area doesn't - that's where I removed tiles):
Please let me know if you'd like me to provide any additional information.
I lost a few hours when I accidentally unrotated my NavMeshSurface2D:
Name your new empty GameObject, name it "NavMesh", and rotate it -90, 0, 0.
https://github.com/h8man/NavMeshPlus/wiki/HOW-TO#nav-mesh-plus
Some ideas for improving this:
I'd be open to contributing this change.
Hello,
As a newbie Unity dev, I found this tool very useful.
One quibble that was frustrating, however, was that the How-To guide is a PDF, which means that within GitHub (where I guess most people will read it) the text is not selectable or searchable (via Ctrl+F).
Is there a reason for this? And if not, would you be open to reworking it into standard markdown? I would be happy to help, if so. Additionally, I believe you are a non-native English speaker, and I think there are a number of things that could be phrased more clearly, if you would be open to letting me rejigger some things there, too?
The biggest downside is that I'm a really amateur Unity dev, so I think there are some fundamental concepts I may be misunderstanding in your explanation, and it may require you to answer some dumb questions on my side. :(
Thanks so much for the great lib.
repro:
result: the area of the tilemap is still non walkable
I'm using ColliderType.Grid for wall tiles and navMesh is building for them correctly. But I also use ColliderType.None for decoration tiles which are not obstacles like grass or bushes or decoration continuation (like spikes) of wall tile in neighbor cell.
But CollectTileSources() method includes such tiles in sources.
Of course, I probably should keep such tiles in separate tilemap to avoid redundant iteration. But anyway, adding ColliderType.None tiles into sources feels wrong.
I have weird navmesh artefacts. You can see them in the screenshot. I managed to fix them by adding a quad rotated (90, 0, 0) and referencing it in Use Mesh Prefab setting of the NavMeshSurface2d. I don't understand why this fixed the problem, but somehow it worked.
But for me this solution feels like just a workaround. I think there should be no artefacts in a first place.
P.S.: thanks you very much for such a great tool
Hey there,
keep getting an error when trying to use these scripts in NavMeshBuilder2d and NavMeshSurface2d because apperantly they are using default literals which is not working for some reason
Hello guy!
I use your asset very useful.
But, I have question about movable obstacle.
Some obstacle object is Instantiate at runtime.
How to bake NaveMeshSurface2D at runtime?
Hi!
Can you add package.json for use sources as Unity Package? Like these https://github.com/Unity-Technologies/2d-extras
I have a player gameobject placed on a navmesh with NavMeshSurface2d rotated -90 on X just like the WIKI says. When the "game" starts the z of the transform holding the player gameobject is set to --0.7708252.
The runtime navmesh generation works, colliding works, agent.SetDestination works.
Manual movement (keyboard arrows) don't work, the player object get's stuck from time to time when moving up or down, probably to the Z missmatch.
As a workaround I have code in the update to set the Z to 0 whenever it's not (the value is getting set to the -0.7708252 constantly actually), but that results in some other issues and just feels bad.
Using NavMeshSurface doesn't generate the issue, but of course doesn't work for the 2d case anyway so quite hard to say if it's really NavMeshSurface2d but I have been randomly disabling components and that's my best bet now.
Let me know if you can help and if I can provide any more clues here.
Cheers
Use physics collider shape for NavMeshBuildSourceShape.Mesh.
It required physics collider polygon triangulation efficient library
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.