Hi there!
I’m new to this project and I’m having some trouble with my AI move behaviour that I hope someone here can help with.
I’m running into an issue where every time I recalculate the agents path to its targetDestination, in a single frame the direction is flipped.
I’m guessing it has something to do with when a new path is calculated, the agent is already ahead of the starting node in the new path, meaning it turns around for at split second or something?
protected override void OnUpdate()
{
if (recalculatePathTick.value > 0.0f)
{
internalTimer -= Time.deltaTime;
if (internalTimer <= 0.0f)
{
internalTimer = recalculatePathTick.value;
CalculatePath();
}
}
if (currentPath != null && currentPath.CompleteState == PathCompleteState.Complete)
{
if (currentWaypoint >= currentPath.vectorPath.Count)
{
EndAction(true);
return;
}
currentDirection = (currentPath.vectorPath[currentWaypoint] - enemy.transform.position).normalized;
currentDistance = Vector2.Distance(enemy.transform.position, currentPath.vectorPath[currentWaypoint]);
if (currentDistance < nextWaypointDistance.value)
{
currentWaypoint++;
}
}
previousPosition = enemy.transform.position;
enemy.Input.SetAxisDouble(InputAction.LeftStick, currentDirection);
}
private void CalculatePath()
{
pathSeeker.StartPath(enemy.transform.position, To.value, OnPathCalculated);
}
private void OnPathCalculated(Path newPath)
{
ABPath p = newPath as ABPath;
if (p == null || p.error)
{
return;
}
currentPath?.Release(this);
currentPath = p;
currentPath.Claim(this);
currentWaypoint = 0;
//justCalculated = true;
}
I’m also using this together with NodeCanvas, which is why I have EndAction() functions etc.
Is there any way to smooth out the positions, whenever the path is recalculated somehow?
Thanks!