Help with "de-exponential" movemnt

I have 2D “de-exponential” acceleration, meaning that at first the player will go from velocity 0 to velocity 5 in 3 seconds, but from 5 to ten in 7 seconds(not the exact numbers, but that is the idea)
My first idea was:

Pvelocity.x += 1 / Pvelocity.x * Time.deltaTime;

Which looks like this on a graph:


The problem, is if the speed is small, the player well get really big amounts of speed.
Make so that the start speed is high will not fix the problem, the player can still
deaccelerate, and when he is at a low speed start moving.
Next, I tried this:

Pvelocity.x = mathf.pow(Pvelocity.x, speed);

Which looks like this:


For this one, I don’t how understand how to make it work, nor how to use Time.deltaTime on this.
Please explain how to use increase the player’s velocity in a de-exponential fashion.

If you’re doing this because you didn’t like your character constantly accelerating then you can add drag. Although the issue with rigidbody drag is that it applies drag on both the X and Y axis resulting in your character falling slowly. So instead you can apply your own drag on just the X axis:

void FixedUpdate()
{
	rb.AddForce(Vector2.right * 5 * Input.GetAxis("Horizontal")); // move the player left and right with a speed of 5
	rb.AddForce(new Vector2(-rb.velocity.x,0)); // add horizontal drag to prevent the player from constantly accelerating
}
1 Like

I would just use a public AnimationCurve object, with the X-axis indexed by your present speed and the Y axis specifying how much to accelerate.

That way you can make the curve visually do exactly what you want in the editor, no more code changing to tweak values.

1 Like

I had no idea that it existed, but how will changing direction/deceleration work?

You could have an acceleration schedule and a braking schedule, or deceleration schedule.

If you want to do it with formulae, such as your first one, and want to avoid the crazy-high initial values, just add an offset to your divisor, such as:

y = a / ( x + b)