How to make a smooth camera follow algorithm?

16,670

Try something simple like lerping a tenth of the distance. It works surprisingly well.

float lerp = 0.1f;
Vector3 position = this.getCamera().position;
position.x += (Obj.x - position.x) * lerp * deltaTime;
position.y += (Obj.y - position.y) * lerp * deltaTime;
Share:
16,670
Avetis Zakharyan
Author by

Avetis Zakharyan

I am awesome. That's the main thing about me.

Updated on June 17, 2022

Comments

  • Avetis Zakharyan
    Avetis Zakharyan almost 2 years

    I am making a game with LibGDX (Java).

    I need the camera to follow a fast moving character. The easiest way to do it is to just write this:

    this.getCamera().position.set(obj.x, obj.y, 0);
    

    But, is there any algorithm to make this more smooth? Like when camera is not that strict, and is always a bit late: character goes quick right, camera follows with slight delay, or if you suddenly appeared somewhere far, camera doesn't teleport instantly but travels at a top speed to you when it comes closer it slows down a bit and finds you again.

    Is there any libgdx libs that do that or anyone had this experience?

  • Mortennobel
    Mortennobel over 10 years
    Suggestion: also multiply with delta-time to make the movement independent of the framerate.