Hello Guest

Author Topic: A* Pathfinding  (Read 10282 times)

zeteginara

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
A* Pathfinding
« on: June 20, 2014, 02:23:15 pm »
Is there a pathfinding solution that is recommended with 2dToolkit that is easy to implement?

I've searched for a while on these forums, and no one seems to have come up with a solution.  There is one that seems to work with http://arongranberg.com/astar/...

But requires all of your tiles to use box colliders instead of edge colliders, which is a lot of manual work and isn't an 'easy solution' at all.  I use a tilemap with smaller tiles, 16x16, and each tile has a custom collider.  I'm okay with defining each tile as 'collidable or not' ***ONLY FOR THE PURPOSE OF THE PATHFINDING SYSTEM***, and I found a solution that ALMOST does the trick with Simply A*, but the mesh, as seen here:



Tends to blend with the collidable area, meaning objects in the game that try to pathfind around a corner will run into a wall.

I think I would also be fine if you could replace the colliders in 2dToolkit with box colliders.  Less efficient, sure, but I'm fine with that.

zeteginara

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: A* Pathfinding
« Reply #1 on: June 24, 2014, 02:13:37 pm »
I resolved this with Polynav:

http://forum.unity3d.com/threads/poly-nav-pathfinding-for-4-3-2d.224962/

It's a simple 2D solution which lets you define polygon colliders as obstacles.

You do have to trace over your existing colliders generated by your sprites with polygon colliders, so it's a bit of work, but it's not too bad.

Unfortunately, if you are using a tilemap with a lot of custom colliders, there is no solution that 'just works', but this is probably the one that can be done the most easily.  Polynav is easy to work with and use, as well.

zeteginara

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: A* Pathfinding
« Reply #2 on: July 05, 2014, 03:15:32 pm »
Sorry to necro a thread, but I thought I'd give an update to this, because A* Pathfinding has historically been a hard thing to implement; obvious to all who are doing a quick search on these forums.  So this is for the benefit of someone doing a search, trying to find a pathfinding solution.  Or something for the author to link for someone who asks in the future.

http://arongranberg.com/astar/

This pathfinding software, The A* Pathfinding Project, has been recommended in the past for being a decent solution for 2D, however, it did not take into account 2D Edge Colliders, which is what 2D Toolkit uses, based on my research.

Well, what was really happening was that the A* Pathfinding Project used the Physics2D API that came packaged with Unity, which didn't take 2D Edge Colliders into account as of Unity 4.3.

Well, as of Unity 4.5.1 The Physics2D API *DOES* detect 2D Edge Colliders, which means, The A* Pathfinding Project, linked above, works with 2DToolkit right out of the box.*

*You do have to do a little bit of tinkering; it works on the XZ axis by default, you just have to change a couple of parameters to get it to work on the XY axis.

Basically, in your grid graph, give it a 90 rotation on the x axis to point it in the right direction.  Make sure 2D Physics is checked, and change the climb axis to Z.  That's it.  :)

dotty

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 65
    • View Profile
Re: A* Pathfinding
« Reply #3 on: July 09, 2014, 11:03:43 pm »
Back before the 4.5.1 change came into effect I wrote a near little script that turned the edge colliders that TK2D uses back into box colliders, runs the map generation for a* and removed the box colliders.

https://github.com/kevdotbadger/BoxColliderGenerator

shiba_nivengo

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: A* Pathfinding
« Reply #4 on: September 12, 2014, 03:51:23 am »
Sorry to necro a thread, but I thought I'd give an update to this, because A* Pathfinding has historically been a hard thing to implement; obvious to all who are doing a quick search on these forums.  So this is for the benefit of someone doing a search, trying to find a pathfinding solution.  Or something for the author to link for someone who asks in the future.

http://arongranberg.com/astar/

This pathfinding software, The A* Pathfinding Project, has been recommended in the past for being a decent solution for 2D, however, it did not take into account 2D Edge Colliders, which is what 2D Toolkit uses, based on my research.

Well, what was really happening was that the A* Pathfinding Project used the Physics2D API that came packaged with Unity, which didn't take 2D Edge Colliders into account as of Unity 4.3.

Well, as of Unity 4.5.1 The Physics2D API *DOES* detect 2D Edge Colliders, which means, The A* Pathfinding Project, linked above, works with 2DToolkit right out of the box.*

*You do have to do a little bit of tinkering; it works on the XZ axis by default, you just have to change a couple of parameters to get it to work on the XY axis.

Basically, in your grid graph, give it a 90 rotation on the x axis to point it in the right direction.  Make sure 2D Physics is checked, and change the climb axis to Z.  That's it.  :)

After reading this post I downloaded A* pathfinding project and tried to create a grid graph. However, it only detects collisions with my characters (which have box colliders) but not with the tiles created by 2DToolkit (which are edge colliders). Have you actually made pathfinding project work with 2Dtoolkit? Is there anything else to do besides rotating the grid -90 on x and setting z as the climb axis?

**Edit: Just found the problem. The collider type for the collision testing has to be sphere.
« Last Edit: September 12, 2014, 04:15:31 am by shiba_nivengo »

Doghelmer

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: A* Pathfinding
« Reply #5 on: October 30, 2014, 11:05:54 pm »
I've been having some major difficulties getting Astar to work with 2D Toolkit.  It seems that the included scripts such as AIPath.cs have been written with a 3D plane in mind, using the XZ axis instead of the XY axis for movement.  Despite my best efforts to modify these to suit my needs, I haven't been able to get it working properly.  Has anyone else been able to accomplish this?  Mind posting the scripts you're using?