# How to apply logarithmic type formula for speed?

I want my character's speed to ramp up using somewhat of a logarithmic formula. I am confused about how specifically I would implement this. I want the character to move fast with a little movement as as the movement gets larger the added contribution to speed is less and less. I said logarithmic function because this kind of mimics the shape of what I am after. Does anyone have an example of how this is done?

As much as I can tell you want something like this:

Target_Speed = some value;
Time = -log(Initial_Speed);

while(runnning) {
Time += delta_time;
Current_Speed = Target_Speed - exp(-Time);

do some other stuff
}


And here is why this algorithm works:

We know logarithmic function exp(x) tend to approach 0 when x goes to negative infinity. So in order to make that logarithmic function become zero we should reduce Time to a very low value, or we can increase Time, and compute exp(-Time) instead. based on the current argument, it's easy to see when Time gets very large, -Time becomes real large negative and so exp(-Time) becomes almost zero.

And for the initial values: Target_Speed is your desired speed, and you can set it as you want. in the first cycle we want the Current_Speed to be zero, so Target_Speed - exp(-Time) should evaluate to zero.

        Target_Speed  = exp(-Time)
=>  log(Target_Speed) = log(exp(-Time)) = -Time
=> -log(Target_Speed) = Time


So simply by setting the initial value of Time to -log(Target_Speed) we will get what we wanted.

public AnimationCurve curve;


Then simply select it in the inspector and use a preset. To change speed, use:

currentSpeed = curve.Evaluate(t);


where "t" can be Time.time/5f or other value from 0 to 1.

If you want a more physical way then use rigidbody and rigidbody.AddRelativeForce(Vector3.forward);

• what library is this code using? – Ali1S232 Jan 30 '14 at 20:36
• Sorry I'm new here, I thought it's a Unity question. My bad. – Slasher Jan 30 '14 at 21:33

each frame do this:

speed += 0.05;
pos += speed;
speed *= 0.99;