# Using Lerp For Camera Movement Smoothing. Help!?

## Recommended Posts

Hello Everyone of the Angelscript Forum,

I am back with yet another weird situation. I am sure you guys know the answer to this one. I am baffled right now. ðŸ˜€

So, I am trying to get a camera in a 2D game follow the player with smooth movement. I got the camera following with smooth movement part working, but the problem I have now is that the player character is at the top left corner of the screen. I cannot seem to get the camera to center the player to the screen. I am not sure what to do. The camera smoothing part is working! How do I get the camera to be over the player character so he is near the center of the screen?

Here is my code:
Â

float time = UnitsPerSecond(10.0f);
float lerp = 0.1f;

vector2 position = GetCameraPos(); <- I think this is a problem because to center the camera on player, I would need this to say "vector2 position = PlayerPos - ScreenSize / 2". But if I use that code with PlayerPos - ScreenSize/2 the camera becomes jittery, but I dont understand why that happens?

position.x = position.x + (player.x - position.x) * lerp * time;
position.y = position.y + (player.y - position.y) * lerp * time;
print(position.x);
SetCameraPos(position);

Â

Anyone have an idea on what im doing wrong here?

Thank you,

DrMeowEyes

##### Share on other sites

I figured it out! I just had to make a slight change from this...

position.x = position.x + (player.x - position.x) * lerp * time;
position.y = position.y + (player.y - position.y) * lerp * time;

to this...

Â position.x = position.x + (player.x - (position.x + GetScreenSize().x/2)) * lerp * time;
Â position.y = position.y + (player.y - (position.y + GetScreenSize().y/2)) * lerp * time;

##### Share on other sites

I'm glad you solved it, but I'm not sure the original question was very relevant to Angelscript itself? It's more related to the engine you're using that implements Angelscript.

## Create an account

Register a new account

• 10
• 10
• 12
• 10
• 33