Adam Brodin
Adam Brodin

Reputation: 71

Libgdx camera not working

I am creating a game where a player is moving upwards constantly and i want the camera to follow this player, i have tried this by using this code: sb.setProjectionMatrix(cam.combined);

    cam.position.x = p.getPosition().x;

    cam.position.y = p.getPosition().y;

    cam.update();

But it does not work. Here is the initizalizaion of the camera aswell:

    cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 

    cam.setToOrtho(false); 

    cam.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0); 

    cam.update();

Thanks for any help! :)

Upvotes: 0

Views: 484

Answers (1)

Jafaska
Jafaska

Reputation: 26

Be careful about the order of the code

Call

cam.position.x = p.getPosition().x;
cam.position.y = p.getPosition().y;
cam.update();

Before

sb.setProjectionMatrix(cam.combined);

Hope this works for you!

Upvotes: 1

Related Questions