AIPath SlowDown Inconsistency

Hey, I was working on some more customised movement, build on top of the AIPath.
//Edit: More changes have been made after doing more experimenting, also read the second post!
While working on getting a better control over the acceleration and deceleration, we noticed the following line:

// This is always a value between 0 and 1.
slowdown = distanceToEnd < slowdownDistance ? Mathf.Sqrt(distanceToEnd / slowdownDistance) : 1;

Where slowdown is set to 1 when the character is NOT supposed to slow down at all.
However further down you ( in the stopping movement part of the code) you set slowdown to 1:

slowdown = 1;
// Slow down as quickly as possible

This looks like a bug to me, where in the original slowdown check, the conditional should be set to 0 when distanceToEnd > slowdownDistance

// This is always a value between 0 and 1.
slowdown = distanceToEnd < slowdownDistance ? Mathf.Sqrt(distanceToEnd / slowdownDistance) : 0;

Correct me if I’m wrong,

Thanks for your time,

A bit more info on the changes made for the smoothing near the end of the path:
In the AIPath - MovementUpdateInternal - L363:

slowdown = distanceToEnd < slowdownDistance ? Mathf.Pow( 1 - distanceToEnd / slowdownDistance,2) : 0;

				if (reachedEndOfPath) {
					// Slow down as quickly as possible
					velocity2D -= Vector2.ClampMagnitude(velocity2D, currentAcceleration * deltaTime);
				} else if (!reachedEndOfPath) {
					velocity2D += MovementUtilities.CalculateAccelerationToReachPoint(dir, dir.normalized*maxSpeed, velocity2D, currentAcceleration, rotationSpeed, maxSpeed, forwards) * deltaTime;

In the MovementUtilities - ClampVelocity - L22

var currentMaxSpeed = maxSpeed * (1 - slowdownFactor);

The intent of these changes was to unify what slowdown really means.
In the original version of AIPath slowdown == 1 means both slowdown as much as you can, but also don’t slow down at all ( the original ClampVelocity script supports this statement).
slowdown went from 0 -> instantly to 1, then slowly decreases as we near the end. only to then go back to 1 for full stop.

With these changes slowdown is 0 and ramps up to 1 while nearing the destination.


The slowdownFactor is a forced reduction of the max speed, however inside the ‘Slow down as quickly as possible’ if statement it already adjusts the velocity to reduce the speed as quickly as possible, so this factor is not necessary. That is why it is left at 1. One could argue that it should also be set to 1 inside the if (reachedEndOfPath && whenCloseToDestination == CloseToDestinationMode.Stop) statement though.

Oh ye I agree, I just felt that it could use some more consistency so less people would get confused over what slowDown means in this context. Allowing more people to understand and change the behavior.

1 Like