I have a feature in my game that should cancel the movement of the AI by setting the target in the “AIDestinationSetter” to null. Even though I successfully remove the transform as the target the the AI will still go the the position the target was set to instead of stopping. I was wondering how I could cancel the movement of the AI mid path?
Hi
You can set ai.destination to an invalid value.
For example
var ai = GetComponent<IAstarAI>();
ai.destination = new Vector3(float3.PositiveInfinity, float3.PositiveInfinity, float3.PositiveInfinity);
If you also want to remove the current path the agent is following you can call
ai.SetPath(null);
What worked for me are those steps in total (if you remove any lines it still tries to move):
- It is not enough to simply set destination and set path to null since AIDestinationSetter will write them over and over again so we disable that
AIDestinationSetter setter = GetComponent<AIDestinationSetter>();
setter.enabled = false;
- Basically the steps that Aron mentioned before
AIBase ai = GetComponent<AIBase>();
var inf = float.PositiveInfinity;
ai.destination = new Vector3(inf, inf, inf);
ai.SetPath(null);