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.

All that said I think my answer is exactly the same as Arne's except with little more information about how it's working!


You can add a curve and manually adjust the acceleration graph.

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);

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

each frame do this:

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

adjust parameters at will.

  • 2
    \$\begingroup\$ You should explain what this is doing and why it works in your answer. \$\endgroup\$ – Sean Middleditch Jan 30 '14 at 1:27
  • \$\begingroup\$ This much I know. But I want a way to specify a smooth formula for ramping down the effect of speed as it gets faster. \$\endgroup\$ – Pladnius Brooks Jan 30 '14 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.