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 ambitiousmustard · Feb 26, 2017 at 01:22 PM · pathfindinglooprecursion

Pathfinding function crashes Unity?

Hey, I've been trying to add this pathfinding function to my program, but when I run it, it just crashes. I've read other threads and this might be an infinite loop, but I don't see where it would be other than the recursion area (and it shouldn't break there because of the if-statement checking if it's out of bounds).

 public void SetPath(Vector3 pos, Vector3 goal, Queue<Vector3> p, List<Vector3> visited, int cost) {
 
         Debug.Log("checking " + pos);
         
         //method which checks if the target tile is outside of the map
         if (core.OutOfBounds((int)goal.x, (int)goal.z))
             Debug.LogError("target tile " + goal + " is out of bounds!");
 
         //if the current position isn't out of bounds and hasn't been visited
         else if (!core.OutOfBounds((int)pos.x, (int)pos.z) && !visited.Contains(pos)) {
 
             Debug.Log("pos is within the limits of the map");
 
             //add tile to the visited list and path queue
             visited.Add(pos);
             p.Enqueue(pos);
 
             //add tile movement cost to total path cost
             cost += core.TileCost((int)pos.x, (int)pos.z);
 
             //if the current position is the target tile and the path is shorter than the current one
             if (pos == goal && cost < shortestPath) {
                 Debug.Log("pos is the target tile");
                 shortestPath = cost;
                 path = p;
             }
 
             //else, check tiles around
             else {
 
                 SetPath(new Vector3(pos.x + 1, 0, pos.z), goal, p, visited, cost);
 
                 SetPath(new Vector3(pos.x, 0, pos.z + 1), goal, p, visited, cost);
 
                 SetPath(new Vector3(pos.x - 1, 0, pos.z), goal, p, visited, cost);
 
                 SetPath(new Vector3(pos.x, 0, pos.z - 1), goal, p, visited, cost);
 
             }
         }
     }
Comment
Add comment · Show 9
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 ninja_gear · Feb 26, 2017 at 04:46 PM 0
Share

Am I crazy or is this function calling itself?

avatar image James2Games ninja_gear · Feb 27, 2017 at 04:53 AM 0
Share

Yes it is It's called recursion. Beautiful yet VERY confusing

avatar image ninja_gear James2Games · Feb 27, 2017 at 12:24 PM 1
Share

Oh, I see. I see. When I wrote my own path finder I just used a while loop. $$anonymous$$uch less beautifu,l I suppose, but it did work as intended.

One question about this method... if the first SetPath() called by itself finds the goal, won't the other three SetPath() directions still be called unnecessarily?

Show more comments
avatar image James2Games · Feb 27, 2017 at 04:47 AM 1
Share

Can you please show how this method is called as well as what shortestPath is initialized with?

Edit: Also how large is your map? If it's too large then Unity may not be able to search it all and can crash.

avatar image ambitiousmustard James2Games · Feb 28, 2017 at 02:32 PM 0
Share

The map is 30x30 right now! I'm hoping to support bigger sizes, though.

avatar image James2Games ambitiousmustard · Feb 28, 2017 at 07:39 PM 0
Share

Can you try it on a smaller map? $$anonymous$$aybe 5x5 to see if it still crashes? If it does then it's not a sizing issue.

avatar image ninja_gear · Feb 28, 2017 at 08:11 PM 0
Share

it seems to me that the method of recursion used here to find a path is going to take a long time to find paths that are not located "north" of its position. You benefit from looking into A* (A Star) or another suitable method to find paths. I know that's what I did and it helped me fix the design problems I ran into when I tried this sort of thing. On a personal aside I found pathfinding to be a really fun program$$anonymous$$g challenge. Good luck with ur project!

avatar image James2Games ninja_gear · Mar 01, 2017 at 12:05 AM 0
Share

Actually you're right. This algorithm will always return the first path to the goal even if it's the longest due to the visited list containing the goal node.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NecrosDk · Feb 26, 2017 at 05:25 PM

Your problem is that every time you SetPath in the new directions, and from there branch it again, and again, and again, each direction doesn't know about the "visited" list that you've filled in the other directions, which means that your program never knows that you've actually visited all positions.

You need to try each direction one at a time, and when you're done with that direction, pass the visited list down to the next direction.

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 James2Games · Feb 27, 2017 at 04:59 AM 0
Share

The visited list is passed as a reference around throughout the recursion so all iterations will use the same list.

This also means that the same Queue p is being used all throughout the recursion.

 SetPath(new Vector3(pos.x + 1, 0, pos.z), goal, p, visited, cost);

p should be replaced with new Queue(p) in order for it to create a new path for each direction the recursion takes. Otherwise it will return the first path that gets to the goal node.

avatar image ambitiousmustard · Feb 28, 2017 at 02:33 PM 0
Share

I'm going to try this as soon as I can :) Thanks!

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

7 People are following this question.

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

Related Questions

Call a function from within itself 3 Answers

Loop crashing unity (pathfinding) 2 Answers

Recursive Tree Loop? How do i make the matrix-array? 3 Answers

Help with pathfinding function! 2 Answers

Logic question, unique path finding system 2 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