# (C#) Accelerating Player Based On Distance

Hello, I’ve been stuck working on this code for a few days now and I’ve made some progress but am totally stumped.

The scenario: A direction is pressed and the player accelerates linearly from 0 to a speed of lets say 3u/s and hits that speed within 0.5 units distance. The time it takes to accelerate is irrelevant.

I understand that the first thing i have to do is calculate the acceleration, which i’ve found multiple contradicting formulas for, and then increment the player’s position based on that acceleration. Would I also have to calculate how long that would take in order to move the player properly? I’ve looked extensively into lerps and simply can’t figure out a solution. Any help is welcome at this point. Thank you!

This is where a good reference point can really come in handy.

By a typical acceleration formula, you’re taking the current speed, the target speed, and the time spent accelerating between those two speeds. With that information, you can determine the distance traveled and the rate of acceleration to reach that point.

This is done using the formulas:

tgtSpeed-curSpeed/time = acceleration

(avgSpeed/2)*time = distance

Since you know the distance, however, let’s take this equation and work backwards, shall we?

distance/avgSpeed = time

Yep, time still winds up being a valuable factor in this. But since the time can be determined by reversing the speed-to-distance equation, you can use that to turn it back around and calculate the acceleration rate (force) to apply to reach the desired speed.
``````public static float AccelerateToTarget(float currentSpeed, float targetSpeed, float distance)
{
float averageSpeed = (currentSpeed + targetSpeed) * 0.5f;
float time = distance / averageSpeed;

float speedDifference = targetSpeed - currentSpeed;
float acceleration = speedDifference / time;
return acceleration;
}

void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
// Example call using your values
forceToAdd = AccelerateToTarget(rb.velocity.magnitude, 3.0f, 0.5f);
}

void FixedUpdate()
{