Search Unity

Smooth Velocity Based Movement

Discussion in '2D' started by BobberooniTooni, May 1, 2021.

  1. BobberooniTooni

    BobberooniTooni

    Joined:
    Apr 9, 2021
    Posts:
    46
    Heyo,

    So I got a 2D game going with drifty and slidey movement, and it's based on velocity, not the direct movement of the player's position. However, it's got quite the jitter on its own and setting the rigidbody2D to interpolate actually makes the jitter worse.

    I've got a smooth camera that can largely mask the jittering of the player and make it appear silky smooth, but of course, that results in other objects not being as smooth as they should/are.

    I'm wondering what I could do to smooth out jittery movement. This is my movement line which takes place in FixedUpdate():

    //move our character in the input direction with the movement speed
    //and maintain part of our previous velocity to avoid abrupt changes
    //and to allow drifting when there is no input
    rgBody.velocity = rgBody.velocity * 0.96f + (input * movementSpeed) / 10;


    From what I understand, things like Lerp smooth based on position, so how could I go about this?

    Thanks in advance for your advice.
     
    CatnanaStudios likes this.
  2. m_vishuu

    m_vishuu

    Joined:
    Mar 5, 2021
    Posts:
    13
    I have tried doing this by changing velocity to a certain value over time and by changing velocity to a certain value with a rate. The later one provides better results for me and doesn't involve lerp. You can achieve the same by making a function to accelerate.
    velocity = velocity + (movingInDirection * acceleration * Time.deltaTime);

    where movingInDirection is an integer that is equal to either 1 or -1, depending on what direction the body is moving in horizontally. Other variables are understood.
    Call this function when you need to change velocity , i.e, drift.
     
  3. BobberooniTooni

    BobberooniTooni

    Joined:
    Apr 9, 2021
    Posts:
    46
    Interesting approach! I’ll give this a go, thanks