Advertisement

Using Lerp For Camera Movement Smoothing. Help!?

Started by March 04, 2019 03:08 AM
2 comments, last by Miss 5 years, 8 months ago

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

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;

Advertisement

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.

This topic is closed to new replies.

Advertisement