Search Unity

Pathfinding

Discussion in 'Editor & General Support' started by Jonathan Czeck, May 6, 2005.

  1. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    How would I go about pathfinding in Unity? I am well versed in strategies such as A*, but there doesn't seem to be a way to get at the actual mesh representations of things in game. I'm thinking in general terms, here. I don't have an actual problem I want to solve. I'm just brainstorming ideas.

    I can think of three flavors of pathfinding problems that would come up:

    A. Pathfinding from one polygon to another on a mesh. (Terrain)
    B. Pathfinding from one location to another while avoiding different tagged GOs.
    C. Some mix of the above two.

    So, will I be forced to do something at the MonoBehavior level, or is there something lower level coming up in Unity that I could use? A* is expensive, so it'd be good to have it written with performance in mind.

    -Jon
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Here is how I would go about it:

    You add pathfinding nodes (GO with a homemade PathNode attached)... They are situated a bit above the terrain (say 1 meter - whatever is suitable)

    On Awake, you cast rays between the pathnodes in the scene. If they can see each other, you have edges of your graph... Probably, you want to clean up the paths a bit after that.

    While sounding cumbersome, you can add pathnodes to, say, your tree prefab and they'll autoconnect to give paths around the the trees connect to each other.

    When you want to do an actual pathfind, you could find the nearest pathnode to source and dest places, and use those. You probably want to use motion smoothing to make it look better, but the basics should work...

    Have only implemented 20% of this (nodes finding each other autoheight above terrain), but that part seemed to work just fine....

    Good Luck. Post your stuff here I'll help out when I need a break from the docs.
     
  3. Sammael

    Sammael

    Joined:
    Dec 11, 2012
    Posts:
    24