how do i use acceleration instead of instant maxspeed

Im creating a little game and i have a simple but working movement script.
The problem is i would like my character to accelerate to the target speed and decelerate after
This is the movement part of my script so far.

    public float Speed = 40f;

void FixedUpdate()
    {
        //left and right input
        float xMov = Input.GetAxisRaw("Horizontal");

   
        //horizontal moving
        rb.velocity = new Vector2(xMov * Speed, rb.velocity.y);
}

Velocity = velocity + acceleration * deltatixedtime.

There are different opinions on this though. That’s the one I most commonly see, just adding to the velocity on a fixed step. Personally, I do an update every single frame to make it look smoother. I record when an acceleration started and know how long it’s been since the, and just solve the differential to know my current speed/positional offset. The calculus is most expensive, but far more accurate.

4 Likes

Input.GetAxis does this internally.
Input.GetAxisRaw, what you’re currently using, does not have any acceleration.

1 Like

You could use Vector2.MoveTowards to move the current velocity ‘towards’ the velocity you want. The maxDistanceDelta parameter would control your acceleration/deceleration rate.

1 Like

Smoothing movement between discrete values:

The code: Smooth movement - Pastebin.com

In your specific case you would use that to adjust the velocity from its present value gradually to the desired value.