GridGraph AiPath acceleration?

Hi Aron, I try to use RecastGraph with acceleration and slowdown but some bug with unit “teleport” don’t give to do it. And I think that GridGraph completely satisfied me except that AiPath don’t have acceleration and slowdown. Would you help me with that? I need accelerate unity to some speed and slowdown it if less than some distance.
ps: I use pro A*.

Hi

Sorry for not answering earlier.

There is no built in support for that, however it can be accomplished by modifying the CalculateVelocity method a bit.
In the end it uses a variable called ‘sp’ (for speed, but that name was already taken) which indicates which speed it wants to use for the current frame. If we clamp this variable so that it is not higher than our speed the previous frame + the maximum acceleration possible we can get the character to slowly accelerate when starting to move (so sp = min(sp, previousSp + acceleration*Time.deltaTime) or something similar). To get it to slow down, the easiest approach is to clamp the sp variable to for example the distance to the target waypoint (potentially with some constants involved to tweak it a bit), if that is done when the current waypoint is the last waypoint in the path, then it will slow down when it is close to the target (so sp = min(sp, distance(current position, target point)) or something similar)

Thank you for reply. For slow down I decided count distance of all path by this code.
float targetDist = dir.magnitude;
float targetDistNew = targetDist;
if (vPath.Count -1 > currentWaypointIndex)
{
for (int i = currentWaypointIndex; i < vPath.Count - 1; i++)
{
targetDistNew += (vPath[i] - vPath[i+1]).magnitude;
}
}
float slowdown = 1f;
bool slow = targetDistNew <= slowdownDistance;
Thank for your idea, you helped me a lot.

1 Like