- Home /
agent.remainingDistance not working properly or am I missing something?
I am making a script that cycles through two different targets. I have made it so when the remaining distance goes below one it changes to the next target. Currently the ai moves correctly from it's spawn location to the first target and remaining distance stays above 1 but as soon as it reaches the first target and remaining distance goes below one the script changes its target to the next location which is way more than 1 unit away but the remaining distance does not change and just stays below one causing the script to start changing the targets back and forth as fast as it can. I have been trying to fix this all day but the same problem keeps happening. The script is below.
private void Update()
{
if (target == null) {
character.Move (Vector3.zero, false, false);
Debug.Log ("Target is null");
}
if (target != null) {
agent.SetDestination (target.position);
}
if (agent.remainingDistance > agent.stoppingDistance) {
}
if (agent.remainingDistance < 1 && firstTargetSet == true) {
if (currentTarget >= targets.Length - 1) {
currentTarget = 0;
}else {
currentTarget++;
}
target = targets[currentTarget];
}
}
Comment