- Home /
Having trouble with A* pathfinding
I've been using A* pathfinding for a while now, but recently it just doesn't want to work. I must've accidentally changed something, but I can't figure out what. My AI seems to calculate the initial path just fine, but then doesn't update its direction afterwards. It just bumps and slides into walls without actually following the calculated path. If I turn on seeker gizmos, that also doesn't seem to show anything. Here's the part of my code that's handling pathfinding itself:
float distance = 0;
reachedEndOfPath = false;
while (true)
{
distance = Vector2.Distance(transform.position, path.vectorPath[currentWaypoint]);
if (distance < nextWaypointDistance)
{
if (currentWaypoint + 1 < path.vectorPath.Count)
{
currentWaypoint++;
}
else
{
reachedEndOfPath = true;
break;
}
}
else
{
break;
}
}
dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
}
I use the seeker.StartPath() method in a different part of my code, and as I said, that part seems to be working fine.
seeker.StartPath(transform.position, closestCover.transform.position, OnPathComplete); The part that handles movement is very simple:
void FixedUpdate()
{
rb2d.AddForce(dir * movementSpeed);
}
I guess it's also worth mentioning that this is a top-down 2D game.
I would be extremely grateful for any help, and let me know if you need any more details, I just wasn't sure what I should include here.
Answer by Dodokom · Jan 31, 2020 at 02:23 PM
Nevermind, my problem was that NextWaypointDistance was set too big. Lowering it completely fixed it.