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
0
Question by Beeblebrox · May 22, 2017 at 03:39 PM · aieditor-scriptingpathfindingstackoverflow

Creating custom pathfinding solution stack overflow

Hey guys, I was attempting to implement a fully 3D pathfinding system for the drone AI in my game. The environments are completely enclosed by colliders. To do this I decided I would create an ScriptableWizard to automatically populate my levels with nodes to later be used in conjunction with Dijkstra's algorithm to enable pathfinding. The basic algorithm is as follows:

  • Start from an initial position within one of the level's rooms.

  • Instantiate a node there

  • Raycast outward in all six directions (forward, back, up down, left,right)

  • If the space to the left, right, whatever is clear, instantiate another node there.

  • Run the same raycast test from this new position

The reason this works is each node itself has a box collider, therefore a new node can never be instantiated on top of a previous one.

Anyways, all this works great for small levels. But if my space becomes too large, I get the following error message:

 StackOverflowException
 UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:63)
 UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:80)
 CreateNavmesh.ComputeNode (Vector3 position) (at Assets/Editor/CreateNavmesh.cs:27)
 CreateNavmesh.ComputeNode (Vector3 position) (at Assets/Editor/CreateNavmesh.cs:36)


My full code is below:

 using UnityEditor;
 using UnityEngine;
 
 public class CreateNavmesh : ScriptableWizard
 {
     public Transform startpos;
     public Transform navmesh_obj;
     public float grid_len = 1f;
     public float boxwidth = 1f;
     
 
     [MenuItem("Navmesh_3D/Compute Navmesh")]
     static void CreateWizard()
     {
         ScriptableWizard.DisplayWizard<CreateNavmesh>("Create Navmesh", "Go!");
     }
 
     void OnWizardCreate()
     {
         Debug.Log("Pressed Button Create");
         ComputeNode(startpos.position);
         
     }
 
     void ComputeNode(Vector3 position) {
        
             Instantiate(navmesh_obj, position, Quaternion.identity);
             if (Vector3.Distance(position, startpos.position) < 50) // My way of making sure this does not get out of control
             {
                 if (!Physics.Raycast(position, Vector3.up, grid_len + boxwidth))
                 {
                     ComputeNode(position + Vector3.up * grid_len);
                 }
                 if (!Physics.Raycast(position, Vector3.down, grid_len + boxwidth))
                 {
                     ComputeNode(position + Vector3.down * grid_len);
                 }
                 if (!Physics.Raycast(position, Vector3.left, grid_len + boxwidth))
                 {
                     ComputeNode(position + Vector3.left * grid_len);
                 }
                 if (!Physics.Raycast(position, Vector3.right, grid_len + boxwidth))
                 {
                     ComputeNode(position + Vector3.right * grid_len);
                 }
                 if (!Physics.Raycast(position, Vector3.forward, grid_len + boxwidth))
                 {
                     ComputeNode(position + Vector3.forward * grid_len);
                 }
                 if (!Physics.Raycast(position, Vector3.back, grid_len + boxwidth))
                 {
                     ComputeNode(position + Vector3.back * grid_len);
                 }
             }
         
     }
 
    
 }

I believe the stack overflow is the result of the really deep recursion resultant from my algorithm. Is there a better way of doing this? Any help would be much appreciated.

Comment
Add comment · Show 1
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 Beeblebrox · May 21, 2017 at 02:57 AM 0
Share

Hey, I figured this out. Your have to use something called the Stack class.

$$anonymous$$y new ComputeNode function looks as follows:

  void ComputeNode(Vector3 position) {
         Stack<Vector3> stack = new Stack<Vector3>();
         stack.Push(position);
         Vector3 currentpos;
         while (stack.Count!=0)
         {
             // Do something
             currentpos = stack.Pop();
 
             // Push other objects on the stack.
 
             Instantiate(navmesh_obj, currentpos, Quaternion.identity);
             if (Vector3.Distance(currentpos, startpos.position) < 50) // $$anonymous$$y way of making sure this does not get out of control
             {
                 if (!Physics.Raycast(currentpos, Vector3.up, grid_len + boxwidth))
                 {
                     stack.Push(currentpos+Vector3.up*grid_len);
                 }
                 if (!Physics.Raycast(currentpos, Vector3.down, grid_len + boxwidth))
                 {
                     stack.Push(currentpos + Vector3.down * grid_len);
                 }
                 if (!Physics.Raycast(currentpos, Vector3.left, grid_len + boxwidth))
                 {
                     stack.Push(currentpos + Vector3.left * grid_len);
                 }
                 if (!Physics.Raycast(currentpos, Vector3.right, grid_len + boxwidth))
                 {
                     stack.Push(currentpos + Vector3.right * grid_len);
                 }
                 if (!Physics.Raycast(currentpos, Vector3.forward, grid_len + boxwidth))
                 {
                     stack.Push(currentpos + Vector3.forward * grid_len);
                 }
                 if (!Physics.Raycast(currentpos, Vector3.back, grid_len + boxwidth))
                 {
                     stack.Push(currentpos + Vector3.back * grid_len);
                 }
 
             }
 
         }
     }


No more stack overflow, even with hundreds of thousands of nodes!

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Have falling object exit from a collider after collision? 2 Answers

Enemy Movement AI in top-down, 2D, tile & turn based game 1 Answer

How do I go about making a 2d platform enemy ai 1 Answer

Pathfinding AI with Aron Granberg, Prefabs with AI not Retaining Target Transform, Trying to change for AI to Follow GameObjects with Tag "Player" PLZ HELP 1 Answer

AI Code Errors 1 Answer


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