Custom Acceleration

Hello, i’ve gotten a project in which i have to simulate a movement with acceleration, constant speed and deceleration. Basically, the user clicks somewhere on the screen, if raycast says it collides somewhere on the floor move character there. The problem is the move, i have a beginDistance and endDistance variables which represent the distance (no specific unit) where i apply acceleration or deceleration.

Basic acceleration equations are :

a = (Vmax - Vinit) / t

d = ((Vmax + Vinit) * t) / 2


a = acceleration

d = distance

t = time

Vmax = maximum velocity

Vinit = initial velocity (always 0 in this script)

which gave me this for acceleration :

float smoothDist;
        smoothTime += Time.deltaTime;
        smoothDist = Vector3.Distance(beginPos, nextPos);
        float totTime = ((2 * smoothDist) / (movementSpeed + 0));
        if (smoothDist < Vector3.Distance(transform.position, nextPos))
            transform.position = Vector3.Lerp(beginPos, nextPos, ((totTime * smoothTime) / 100));

beginPos = position where acceleration begins

nextPos = position where acceleration ends (this method has 3 parts)

In short, in this version i try to calculate how much time is needed to end the acceleration part, then just move transform to the corresponding % of the distance. Which works, but there’s no acceleration. After trying different ways of using the equations, i’m out of ideas. Does anyone know how to do this?

Several ways to do this. SmoothDamp might be the easiest, as it acts like Lerp but accelerates rather than using a constant speed. If you're using rigidbodies, you could also do this by using AddForce, though you'd need to deal with the three states, accelerating to speed, cruising at speed, and decelerating back down to stop, and also remember the edge case where the trip is too short to ever reach the cruising speed!