Idle AI walking around

Hello A* community,

I’m new to both, this forum and A* Pathfinding project.

Prior to posting this question I looked at this documentation page and searched the forum a bit but couldn’t find the answer. Forgive me if it’s still there, as I learn the ropes of this plugin I’ll hopefully pester you all less.

I’m trying to make my enemies wander around when the player isn’t nearby, instead of remaining in one place.
I’m using a recast graph as my level is randomly generated. So what I’d like to do is get a walkable point on this navmesh within a certain (adjustable) distance from the enemy and make the enemy move towards that point.

I tried using GetNearest but I guess I don’t understand how exactly “nodes” work on a navmesh, as well as how to actually move the AI; the way i’m currently chasing the player is by calling RichAI.UpdatePath() but I don’t quite understand where exactly does the movement happen in seeker.StartPath as the callback isn’t specified and I don’t see any other code doing the actual movement. If you could also explain this, that would be helpful.
Also, it says that GetNearest is very slow, is there any optimized solution for this?

Thanks!

Hi

seeker.StartPath will send a path calculation request and then later it will call the OnPathComplete method in the RichAI component. This is set using these lines in the RichAI script

//Make sure we receive callbacks when paths complete
seeker.pathCallback += OnPathComplete;

GetNearest(Vector3) is not that slow, it is pretty fast. However the GetNearest(Ray) overload is very slow but I doubt that is the one that you would use for this.

One way to do what you want would be something like this

 var rndPoint = enemy.position + Random.insideUnitSphere * 10;
 var closestPointOnNavmesh = AstarPath.active.GetNearest(rndPoint).clampedPosition;
 richAI.target.position = closestPointOnNavmesh;

Ah, I registered my own OnPathComplete during grid tutorial but then couldn’t find it actually registering to pathCallback in the RichAI script, thanks Aaron. I guess it then proceeds to create the calculated path through rp.Initialize() and then goes from there.

So how exactly should I be calling the function to move towards target? I’ll adjust target’s position or plugin in a Transform (like player) and then I should call RichAI.UpdatePath()? If no what should I call instead, if yes, should it be called on every tick or only once until the target is reached? I sometimes get the warning “path canceled because another path was requested” and I figure that’s because of UpdatePath being in Update()?

Another question, I also couldn’t find a function which is called when the target is reached, could you please point me to it?

Thanks for your help Aaron : )

EDIT: so far what i’ve done is I’m only callind UpdatePath when the target’s position changes, and that seems to do the trick; it moves the A.I. and doesn’t throw any more “path cancelled” warnings. Am I doing it correctly?
I still can’t find the “when target reached” function though

Hi

The RichAI script periodically recalculates the path, so after you have moved the target transform you don’t need to call any method at all. You may call UpdatePath to ensure it calculates the path immediately instead of waiting until the next scheduled time. If it was currently calculating a path it will log that warning, but that’s nothing to worry about if you are not changing the target all the time (because then most path requests that are started will never complete because you are aborting them all the time).

The RichAI has a virtual method called OnTargetReached. You can subclass the RichAI class and override that method.

damn it I feel like a blind man, i was looking for a method with that or similar name all over the place and somehow didn’t see it. Also Monodevelop’s search function is buggy and it didn’t show anything <_< Thanks!

If I want to stop the A.I. from moving, should I just change its target to it’s own position or is there a method to cancel its movement once the UpdatePath has been called?

EDIT: I found the Stop function in the AIFollow but it says that one’s deprecated and I’m using RichAI and there I can’t find anything to stop the A.I. yet

Hi

You can simply set the speed to zero, or disable the component or set the target to the position of the character itself. Either one would work, however setting the target to the same position as the character may make it move forward a small distance until the new path is calculated.

Thanks for response Aaron, i replied in another topic since i figured i shouldn’t be asking unrelated questions in there same thread