- Home /
The question is answered, right answer was accepted
[C#] How to make a pathfinding system for a board game?
So I've been trying to get this for a while now and I've revisited the issue a number of times. Take a look at the picture below:
The white lines are debug lines showing how the waypoints are connected. Each waypoint has an up, down, left, and right GameObject variable in a C# script that I manually assign in the Inspector.
I've been racking my brain trying to think about how to actually calculate movement through this setup. I had an old version of this that basically sent a pathfinder GameObject through the waypoints based on the movement number the user rolled with a dice. It would duplicate itself when I ran into a waypoint that had multiple connections. This worked properly except there was no way to return the different paths. Since the pathfinder was duplicating itself at every necessary branch, when it reported back to the player about it's current location, it would overwrite the list held by the player.
If I didn't have it report the list back, it still worked but then the problem became that the player was required to move each step individually. I want the player to be able to do that if they choose but I also want the player to be able to just click on an ending waypoint and have the character move through the waypoints to get there.
Has anyone done this before? Thoughts on how I might go about it? I've thought of trying to add everything to a list or multidimensional array but I run into one of two problems. Either the array constantly gets overwritten or the waypoints are added with no particular order so it's useless at that point.
Thanks! -Kaze-
Okay, so here is what I think you should do. This is mostly theory because I dont have time to write code:
Go get the A* Project by Aron Granberg. Free version, doesn't matter. Generate a grid on your map. You might want a manual grid so you have full control of the points! Use tags or something to make everything outside the actual board unwalkable, so you just have single lines of nodes. When the player clicks you find the closest node to where the player clicked. Now you set the final target to the clicked node. Next good thing about the A* project is that you can now get all the nodes in the path from the player to the final target. Now you can do calculations based on the previous information. Each node is a move. You don't need to keep track of entire trees etc. Everything you mentioned is easily acheivable trough this, I think, so if you have any questions you can just ask right here or pm me :)
Interesting...I'll take a look at the A* stuff. I tried it a while ago and it kept giving me errors so I scraped it and haven't really gone back.
I use the Unity Navigation system now for some of my stuff...any idea of that could work in a similar fashion to what you described with A*?
EDIT: I think I just had an idea...I'll try it and get back to you!
Answer by KazeEnji · Jul 12, 2015 at 03:42 AM
I figured it out! My setup has a lot of if statements in it...going to need to see if I can simplify it. Basically, I've created a pathfinder for my specific type of board that doesn't need any actual pathfinding object, it just throws a variable through the scripts nearly instantaneously. I'm so excited!