Hello,
I have a point graph in my scene that I load dynamically and I have an animal that is supposed to flee.
I’m using this code: Animal run away
When my player gets close to the animal, I get one the Debug.Log from A*:
Path Completed : Computation Time 5.00 ms Searched Nodes 2 Path Length 2
Path Number 1 (unique id)
But the animal doesn’t move. If I try to check the status of the path I get “NotCalculated” and if I try to use any member of that path variable I get a null object error.
The animal has a RichIA and Seeker component attached.
Any idea what could be the problem?
Thanks!
Hi
Note that when you first create the path, it will not be calculated. Paths are calculated asynchronously, so it may take a frame or several. However the SetPath call that you make will start the calculation.
The most likely issue I can think of is that your search distance is too low, in the linked post that’s the 1000 * 20 constant. You can try increasing that.
Thanks for the quick reply.
I have tried to increase that considerably and no changes. If I check the graph in the editor I see how one path gets highlighted in green in, so I’m assuming it found a path?
am I supposed to call any other method to start moving the agent, or ai.SetPath(path) should do it once calculation is done? (ai.canMove is set to true).
Thanks!
Are you sure you also set ai.canSearch = false to prevent it from automatically recalculating its own path (to the point indicated by the destination property)?
Thanks again for the reply.
This is the component
This is how the path looks in the editor after being calculated
And this is the code:
float l_dist = Vector3.SqrMagnitude(II_PlayerHandler.m_instance.transform.position - transform.position);
if (l_dist < 10)
{
// Disable the automatic path recalculation
m_ai.canSearch = false;
if (!m_ai.pathPending && (Time.time - m_fleed_time) > 5 )
{
var pointToAvoid = II_PlayerHandler.m_instance.transform.position;
// Make the AI flee from the enemy.
// The path will be about 20 world units long (the default cost of moving 1 world unit is 1000).
path = FleePath.Construct(m_ai.position, pointToAvoid, 1000 * 1000);
m_fleed_time = Time.time;
m_ai.canMove = true;
m_ai.SetPath(path);
}
}
else
{
m_ai.canSearch = true;
}
Hmm… Well, that all looks correct as far as I can see. Does movement work if you just use say a AIDestinationSetter component (or just set the destination property) without trying to use a FleePath?
No, it doesn’t want to move (removed my code and added AIDestinationSetter with a target).
I generate the pointgraph before the execution and save it scaned as an asset, then I load it when loading the level like so:
AstarPath.active.data.DeserializeGraphs(m_astar_path.bytes);
Not sure if that has something to do with it.
Hmm. Okay, so it’s not the FleePath part that is a problem then.
Oh wait. I just noticed. You are using the RichAI script, that script is specifically for navmesh based graphs (i.e. navmesh and recast), it does not work for point graphs at all (I thought it logged an error about that, but maybe it doesn’t). On a point graph you should use the AIPath or AILerp scripts. See https://arongranberg.com/astar/docs/movementscripts.html
That was it! Thanks!
Sorry I didn’t read that documenation before.
1 Like