Kevin Bryan
Kevin Bryan

Reputation: 1858

How to create smooth camera?

I made a game using Libgdx where the character is being followed by the cameras, but the problem is that when my character moves the camera shakes.

velocity.add(0, gravity);
velocity.scl(deltaTime);
position.add(velocity.x, velocity.y);
velocity.scl(1 / deltaTime);

my character moves upward because the gravity is 9.8 and when the player touches the screen I set the velocity.x = 100 and velocity.y = -120to make it go downward and forward, that' s when the camera starts to shake.

camera.translate(100 * deltaTime, 0);

I played with the values of camera shake but its either too fast or too slow and there's always a shake. What I wanted is a smooth camera with a delay effect when my character start to move.

Upvotes: 1

Views: 440

Answers (1)

Jim
Jim

Reputation: 1005

You are not setting the camera to follow the player. The player and the camera moves the same direction but the camera is not depending on the player. Also try setting the cameras position instead of translating.

camera.position.x = player.position.x - gamewidth/2;
camera.position.y = player.position.y - gameheight/2;
camera.update();

For a delay effect you should move the camera towards the player position instead.

Upvotes: 1

Related Questions