In my game I use AStar to attack enemies. These enemies also use AStar.
For my player I have this script that runs on update.
There are times when the player is just stuck with a green path still in gizmo but it will not move at all. I use a layered grid graph and this happens about 5% of the time, but it really ruins the gameplay because I use AStar for each and every player attack.
Video of bug : (happens in the middle of it ) https://streamable.com/w8ictg
/// <summary>
/// Updates the path to the current pathfinding target, this runs every frame
/// </summary>
void UpdatePath()
{
// only ai move if we have a target
if (currentTarget == null)
{
UnlockPlayerInput("FreeFlowCharacterController");
astarTravelTime = MAX_ASTAR_TRAVEL_TIME;
ai.canMove = false;
return;
}
astarTravelTime -= Time.deltaTime;
// we have a target
ai.canMove = true;
ai.destination = currentTarget.transform.position;
if (RepathRateTimer > 0)
{
RepathRateTimer -= Time.deltaTime;
}
else
{
RepathRateTimer = 0.2f;
ai.SearchPath();
}
animancer.SetFloat("InputMagnitude", 1.0f);
RotateTowardTarget(currentTarget.transform.position, 10.0f);
if ((ai.reachedDestination || Vector3.Distance(transform.position, ai.destination) <= 1.5))
{
// start the attack
onReachedDestination();
} else if (astarTravelTime < 0)
{
// failed to reach target!
onReachedDestinationFail();
}
}