Search Unity

Having Trouble with A* Pathfinding

Discussion in 'Scripting' started by Narkata55, Apr 28, 2017.

  1. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
    Hey guys, so i've been reading up on A* pathfinding and watching tutorials but I just can't seem to understand a few parts. If my game is a 2D TBS and my grid map consists of a bunch of game objects (imported from Tiled), how do i sort through them correctly and efficiently? The examples i see all seem to define a node class and create a grid (ex: array grid [,]) that seems to store the tiles/nodes in order of their x and y coordinates, so basically im confused on whether or not i need the nodes or grid classes/objects or if it should work using an array of my custom game objects? Thanks in advance!
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    I use this to do pathfinding (https://arongranberg.com/astar/) and it works really well. I've got a bunch of custom game points in the map too and you can just add them to a custom pathfinding map as nodes (if you use a PointGraph type for your map). Then you can just ask the system to find a path between Point A and Point B and it'll return it for you.
     
    lordofduct likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    If you want an out of the box solution that handles grid pathfinding, I can also vouch for arongranberg's astar project.

    If you want to roll your own, it's a bit up to how you want to do things. Storing the x/y-coordinates of the grid as the x/y coordinates of the array is a very good idea, as that simplifies things a lot. The alternative is to create some kind of linked structure (where each tile has an array of tiles next to it), and A* over that.

    For what the array is of - probably not your GameObjects! Probably just a simple type that designates walkable/not walkable and so on. You can link them to your actual GameObject by just having a reference if that's needed.

    It's kinda hard to give advice on how to build your entire thing, so try something and get back when you get stuck.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Is there any reason why you do not first use Unity's built in agent navigation? That uses A* as well and it's built in. Honestly, it seems you are way over your head and missing all the built in features.

    Honestly - use the built in features. You will know when or if you ever outgrow them.

    https://docs.unity3d.com/Manual/Navigation.html

     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    OP is asking for a grid graph, which the Navmesh system is not. Trying to use the navmesh as a grid would just cause a ton of pain.

    Also, you would not use it because it is utter S***.

    It refuses to output any useful information whatsoever, both about the navmesh and the agents. You don't get to edit the mesh. You don't get to modify how agents move through a path other than editing a few variables and hoping that it works. The agent local avoidance is garbage - they will rub against each other no matter what you do.

    Want to know that your agent just turned a corner to play an animation? Sorry, no way to get that information out, so you'll have to

    There's some improvements in the works, but it's still years away from being comfortable to work with. Next to the animator, it's the number one source of bugs and glitches I've had to resolve the last two years of my life.

    The built-in navmesh is a gigantic pile of tech debt, both for unity and for any projects that include it. It's just really bad.
     
    VirtusH and lordofduct like this.
  6. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    The clearest explanation I've found for A* is from Red Blob Games. The articles begin with Breadth First Search and show an evolution from there to A* and why. There are also implementation details given for various languages.
     
  7. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
    Wow, thanks for all the replies, i really appreciate all the feedback and ideas! I ended up looking at some of the articles and url's linked and made my own version of a* using my own custom objects as nodes! Have a great day everybody!