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);
}
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.
You could use Vector2.MoveTowards to move the current velocity ‘towards’ the velocity you want. The maxDistanceDelta parameter would control your acceleration/deceleration rate.