unity – Smooth movement in point-to-move 2d game based on velocity


I have a real-time point-to-move 2D game with one playable character to control.

I want to create smooth player movement using physics and appropriate velocity.
Basic setup:

  • The player has a maximum speed
  • The player has appropriate acceleration and deceleration parameters

How I achieving the required behavior right now:

// float currentVelocity = 0
// const float acceleratePerSecond = 35
// const float deceleratePerSecond = 35

// ...

Vector2 positionToMoveTo = pointToMove.position;
Vector2 playerPosition = playerRigidbody.position;
Vector2 point = positionToMoveTo - playerPosition;

if (point.magnitude > 1) // If player is far enough
{
    if (point.magnitude >= currentVelocity) // Hack for smooth slow down
    {
        currentVelocity += acceleratePerSecond * Time.fixedDeltaTime; // Accelerate a little
    }
    else
    {
        currentVelocity -= deceleratePerSecond * Time.fixedDeltaTime; // Decelerate a little
    }
}
else // If player is close enough - stop movement
{
    currentVelocity = 0;
    pointToMove.isActive = false;
}

currentVelocity = Mathf.Clamp(currentVelocity, 0, playerService.MaxSpeed); // Clamping to fix max speed
playerRigidbody.velocity = currentVelocity * point.normalized;

This code is working but it is really hacked on the second if statement. Closer to end point player starts shaking, when the current velocity increasing in the first frame and decreasing in the next frame many times.
So the question is: How can I achieve the required smooth point-to-move movement using velocity vector?

Why I need to use velocity and can’t use plain MovePosition of Rigidbody2D?
That’s because I rotate a player based on that velocity. It is bad to rotate a player based on pointToMove position because the real front of the player is the current velocity vector.

Some test briefs:

  1. If object is far enough:
   acceleration        max speed        deceleration
|-----------------o-----------------o-----------------|
                 _____________________
        ________/                     ________
 ______/                                       _______
/                                                      
  1. If object is closer:
    acceleration      deceleration
|-----------------o-----------------|
               ______
        ______/      _______
 ______/                     ______
/                                   

Appreciate any help. Thank you.