How do I use my code with the A* script?

First you need to place one instance of the AstarPath.cs script in your scene, this component will do all the computing.
Then on the object you want to find the path for (your player, enemy or similar), attach the Seeker script and your movement script.

Important : If you want to use Javascript to send pathfinding calls, you need to place the ‘Pathfinding’ folder inside the Standard Assets Folder, you don’t need to do this if you only want to receive completed paths

In your movement script, add this when you want to find the path, for example when the player clicks on a spot on the ground in a RTS game.

C#
(GetComponent (typeof(Seeker)) as Seeker).StartPath ( startPoint, endPoint);
Javascript
GetComponent (Seeker).StartPath ( startPoint, endPoint);
The function does not return anything, so how do we know where to go?
The script will process the request and send the completed path back as fast as it can.
To get the completed path, add this function to your code:

C#
public void PathComplete (Vector3[] points) {
//The points are all the waypoints you need to follow to get to the target
}

Javascript
function PathComplete (points : Vector3[]) {
//The points are all the waypoints you need to follow to get to the target
}

Then it is just for your enemy/droid/whatever to follow the points, an example of this can be seen in the AIFollow script in the example project.

Other messages sent by the Seeker script

If you want to know when the pathfinding returned an error, you have to set the On Error variable in the Seeker component to Error Message and add this function to your code:

C#
public void PathError () {
Debug.Log ("Oh no, the pathfinding returned an error!!");
}

Or you can set On Error to Empty Array and the script will send a PathComplete message but with an empty array.

If you have any questions please post them as a comment or mail me at aron[dot]g[at]me[dot]com

60 Responses to How do I use my code with the A* script?

  1. AlbertoZ says:

    Jason,

    You are amazing, thanks for this java translate!

  2. Lucas says:

    Hey ho..!

    First of all, thanks for the effort that you have put in building and mantaining this code, the world needs more people like you! :D

    Now,

    I’m trying to implement your code into my (first) game.
    I got the tutorial version to work without problems, but now i’m trying to tweak it: I’m dealing with a “naval simulator”, with floating buoyant objects. For this reason i can’t have a plane with a collider, because this would ruin the buoyancy effect of the vessels.

    My problem here is that if i remove the collider from the plane, my “player” object just falls through my “water layer”.

    I believe that the problem could be in the fact that the tutorial script requires the usage of a character controller, that will just fall if not obstructed..

    I tried to add a script that forces the position.y of the object at 0 at every Update cycle, but this got the whole thing messed up, and even if the pathfinding seems to be calculated, the player simply won’t move.

    Any idea about how could i use your script, without relying on a character controller?

    Thanks in advance..!

  3. Lucas says:

    Ha! Ignore the previous mex man, I got it to work..!

    I just used Physics.IgnoreCollision, and it works as a charm! :D

    Thanks for the great script!

  4. stefan embleton says:

    I have come across a strange problem. I set up a terrain navmesh and i have a robot running thru 4 way points and when it reaches each one it calls PathComplete then picks the next waypoint from an array until all waypoints have been reached. My first attempts worked fine. Then on one attempt i got an error from StartPath… nullreference on line 599 of AstarPath. once that error has shown up the project never works again no matter what i do..restart the whole machine same result. Then i discovered that if i do a build then run the project again it works fine, sort of, PathComplete never gets called now. All my code is in Javascript. Im running Unity 2.6 on an iMac Quad. Plenty of power , plenty of RAM… please free to use my email if you want to discuss this… A* shows a lot of potential and i would love to help debug this.

  5. Arno says:

    I love the a* pathfinding. It does wat it say it does. (i only get errors with angry ants)
    I’am a newbie and i just don’t know where to start, so i hope you would give some more info.
    I want a character (not my player) to move from one waypoint to another waypoint.
    So i made a character setup the A*, applied the seeker and the AIfollow.
    But i dont know where to start to make a script to follow from waypoint to waypoint.

    Would you please help….

    I think there are more people out there who want to know this…

  6. Admin says:

    Hi Arno

    if you want an example script you can take a look at the Clicker.cs script which is in the example project.
    But in a nutshell, the only code you need is (C#):
    public Transform myFirstWaypoint;
    public Transform mySecondWaypoint;
    public void Start () {
    (GetComponent (typeof(Seeker)) as Seeker).StartPath (myFirstWaypoint.position, mySecondWaypoint.position);
    }

    This will instruct the character to move from myFirstWaypoint to mySecondWaypoint (works best if the character is already at the first waypoint).
    The code assumes that it is in a script attached to the character.

    The syntax for the calls can be found on the page you have commented on, but you have probably read those already.

  7. Colin says:

    Man! Just found your website today! You are a life saver! This is amazing!

    But, I was wondering how I would implement this into a game like your Massive demo level.

    I want to use a texture map to make sure the seekers don’t go on places they shouldn’t, but I don’t know how to make them run to the 1st seeker they see and have the two engage in a believable fight.

    I have the fight code working but how would I make the seekers move from the opposite ends of the map without making their maxViewingDistance incredibly high?

  8. Admin says:

    I have the fight code working but how would I make the seekers move from the opposite ends of the map without making their maxViewingDistance incredibly high?
    I don’t really understand you question, could you please specify it a bit more?

    Sorry for answering late, I have been (and am) on holiday, but I’m home for one day so I will try to answer as many comments as possible.

  9. Oleg says:

    How can i recalculate grid after adding some buildings during the game?

  10. Admin says:

    HI Oleg

    The best way is to call the (so far undocumented) function called SetNodes.
    You can find an example in the Clicker.cs script.
    Basically you first instantiate the building, then you call
    SetNodes (false,myBuilding.colldider.bounds,true);// (walkable, Bounds, fullPhysicsCheck)
    I think that’s the syntax anyway…don’t remember… check the Clicker.cs script.
    If you use fullPhysicsCheck then it will look exactly as if the building was there since the beginning.
    if you don’t use fullPhysicsCheck the script will just use some approximations instead of the real world data.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>