Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Rizzutp · Mar 16, 2018 at 12:08 AM · navigationprocedural meshnpcagentmaze

How to generate Nav Mesh floor in a procedural generated maze?

Hello everyone, I'm new to Unity and I'm trying to add a Nav Mesh to the ground of my procedural generated maze (with some modifies of mine, I followed this tutorial: https://www.youtube.com/watch?v=OzENv_ZRA1g). I checked out in the internet and unfortunately it seemed that Unity doesn't support a dinamic Nav Mesh generation, but then I found out this: https://github.com/Unity-Technologies/NavMeshComponents which contains LocalNavMeshBuilder and NavMeshSourceTag scripts (watch this: https://www.youtube.com/watch?v=n6ilxlcNpgM).

I tried to attach these scripts to some of my components, generated dinamically during the build of the maze, in order to have a Nav Mesh floor, which was previously generated by instantiating many "little squares" ( cubes with a very low height) and parenting them to a "floorHolder" father.

This is NavigationGen function, contained in my Maze generator script, within the Start section.

     void NavigationGen() {
         Vector3 mazePos;
 
         mazePos = wallHolder.transform.position;
         NavMeshGenClone = Instantiate(NavMeshGen, mazePos, Quaternion.identity);
         NavMeshGenClone.AddComponent<LocalNavMeshBuilder> ();
         NavMeshGenClone.GetComponent<LocalNavMeshBuilder> ().m_Tracked = NavMeshGen.transform;
 
         floorHolder.AddComponent<NavMeshSourceTag> ();
 
     }

But when I run my scene, this solution doesn't work, and my NPC agent (a skeleton soldier) is unable to recognize a Nav Mesh floor. The error message is the following: Failed to create agent beacuse it is not close enough to the NavMesh.

Do you know how can I use proficiently these two scripts in order to make the floor of my maze a Nav Mesh? Thanks a lot!

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rizzutp · Mar 17, 2018 at 11:41 AM 0
Share

UPDATE: Using a Parent object for the whole maze and applying the Nav$$anonymous$$eshSurface baker, I obtained this result. Here is the image, available from my Google Drive since I had some problems to upload it here on Unity Answers: https://drive.google.com/file/d/1WZF$$anonymous$$NNX81yd34Z6u1fAf5OI05EgNSZEh/view?usp=sharing

The problem is that the walls are not considered as obstacles, so the whole is blue colored. How can I fix this issue?

avatar image Harinezumi Rizzutp · Mar 17, 2018 at 12:51 PM 1
Share

Great screenshot, it resolved all the questions I was going to ask at first! :)
The walls are deactivated, is that so when you are baking the navmesh? I just created a small test, and if the wall is deactivated when you bake the navmesh, it will not be taken into consideration.
UPDATE: here is the test project in google drive, maybe it can help. It is a similar setup, generating a (non-sensical) maze, then baking the navmesh.

avatar image Rizzutp Harinezumi · Mar 17, 2018 at 02:29 PM 1
Share

Yeah, I did something similar right now, and the Nav$$anonymous$$eshSurface seems to work fine! However, my skeleton soldier seems always stuck in a certain point... maybe I should open another question for the Nav$$anonymous$$eshAgent! xD

Show more comments
avatar image Dargor87 · May 04, 2019 at 06:30 PM 0
Share

Hi @Rizzutp, I also create the maze like you automatically, but I can't add the navmesh to it. Could you please help me solve the problem? $$anonymous$$any thanks in advance.

avatar image Harinezumi Dargor87 · May 05, 2019 at 08:02 AM 1
Share

Hi @Dargor87, I have updated my answer with example code, try to use it for your solution.

Also, please do not write "I have the same problem" as answers, add them as comments. People who are interested in this questions will get notified and can look into your case.

avatar image Dargor87 Harinezumi · May 05, 2019 at 07:46 PM 0
Share

Hi Harinezumi, thank you very much for the suggestion. I tried to use your code but, I didn't understand why, it gives me an error on nav$$anonymous$$eshComponents (it wants to make me generate it either as a variable or as a class). Regarding its use I don't understand how it works, should I use it where I go to generate the maze or should I attach it to the same gameobject to which the script that generates the maze is connected? $$anonymous$$any thanks in advance.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Harinezumi · Mar 16, 2018 at 01:14 PM

You can try using Unity NavMeshComponents (the manual page is here).

Unfortunately, the info on it is still lacking, but you can get the basics from the above pages and here. Basically once you have your maze constructed, use a NavMeshSurface component to collect the geometry (either colliders or meshes) to use in your NavMesh, then use then call Bake() on it. It may take a while for it to complete, partly depending on the complexity of your maze.

UPDATE

Here is a bit of code that will generate a navmesh from all the objects added to it for all the agent types you have defined.
Usage: set the list of game objects that are part of the navmesh with SetNavMeshElements() , then call BuildNavMesh(). Make sure that it runs before any of the NavMeshAgents are enabled, otherwise they will throw an error.

 public class NavMeshGenerator : MonoBehaviour {
 
     [SerializeField] private GameObject navMeshRoot = null;
 
     private List<GameObject> navMeshElements = new List<GameObject>();
     public void SetNavMeshElements(List<GameObject> values) { 
         navMeshComponents.Clear();
         navMeshComponents.AddRange(values); 
     }
 
     private void Awake () {
         if (navMeshRoot == null) { navMeshRoot = new GameObject("NavMeshRoot"); }
     }
 
     public void BuildNavMesh () {
         int agentTypeCount = NavMesh.GetSettingsCount();
         if (agentTypeCount < 1) { return; } 

         for (int i = 0; i < navMeshElements.Count; ++i) { navMeshElements[i].transform.SetParent(navMeshRoot.transform, true); }

         for (int i = 0; i < agentTypeCount; ++i) {
             NavMeshBuildSettings settings = NavMesh.GetSettingsByIndex(i);
             NavMeshSurface navMeshSurface = environment.AddComponent<NavMeshSurface>();
             navMeshSurface.agentTypeID = settings.agentTypeID;
 
             NavMeshBuildSettings actualSettings = navMeshSurface.GetBuildSettings();
             navMeshSurface.useGeometry = NavMeshCollectGeometry.PhysicsColliders; // or you can use RenderMeshes
 
             navMeshSurface.BuildNavMesh();
         }
 
     }
 
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rizzutp · Mar 16, 2018 at 01:35 PM 0
Share

Basically it is what is reported in the tutorial posted previously, I think. However, thanks for linking the relative documentation pages, it was really appreciated ;-)

avatar image Rizzutp · Mar 16, 2018 at 03:21 PM 0
Share

I'm trying to collect the floor squares in a Nav$$anonymous$$eshSurface array, but I'm obtaining this error message: Cannot implicitly convert type UnityEngine.GameObject[] to UnityEngine.AI.Nav$$anonymous$$eshSurface[]. How can I populate through script an array converting game Objects to Nav$$anonymous$$eshSurface type?

avatar image Rizzutp · Mar 16, 2018 at 03:46 PM 1
Share

Ok, maybe this could save my day

     for (int a = 0; a < objects.Length; a++) {
         surfaces [a] = objects [a].GetComponent<Nav$$anonymous$$eshSurface> ();
     }


avatar image Rizzutp · Mar 16, 2018 at 04:58 PM 0
Share

No, it didn't work :-( Any help? Ah, I forgot to say that even my floorHolder is runtime generated

avatar image
1

Answer by tormentoarmagedoom · Mar 16, 2018 at 10:03 AM

Good day. You should check this tutorial and this youtube videos.

Enjoy!

 Accept the answer if helped :D

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rizzutp · Mar 16, 2018 at 12:55 PM 0
Share

I'll check it out! Thanks. Are you sure that it will work with a floor constituted by so many little squares? $$anonymous$$aybe I should unify all the floor meshes in a unique single mesh? These are the things that worry me the most

avatar image Harinezumi Rizzutp · Mar 16, 2018 at 01:15 PM 1
Share

It should work with small squares as well, but the baking time will probably be a lot faster if you combine your squares before baking.

avatar image Rizzutp Harinezumi · Mar 16, 2018 at 01:25 PM 0
Share

Am I supposed to bake all the single squares before to apply the NavigationBaker script? P.s.: I'll try both procedures and check what happens in terms of performances :-)

Show more comments
avatar image
0

Answer by Rizzutp · Mar 16, 2018 at 08:51 PM

alt text

UPDATE: I succeded in apply the Nav Mesh Surface to every single floor cell, but the Nav Mesh Surface is completely translated far away from its cell and I didn't understand why. Suggestions?


navmesh.png (474.4 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Mar 16, 2018 at 10:08 PM 1
Share

You can set the Nav$$anonymous$$eshSurface's center to be the same as your maze's center (position). You will need to either use world space or local space of them - it is not clear from the documentation which one it should be.

avatar image Rizzutp Harinezumi · Mar 17, 2018 at 12:19 AM 0
Share

Is there any chance to set Nave$$anonymous$$eshSurface's center from script, since I baked every single floor cell in the editor to achieve the result reported in the previous image?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making AI that can navigate through a city? 3 Answers

Making NavMesh areas? 0 Answers

How to prevent NavMeshAgents from colliding when warped to same position? 0 Answers

Why Nav Mesh Agent has been baked intermittently on 1 straight component? 0 Answers

How to change a NavMeshAgent angular rotation speed, 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges