Using libGdx
, so Java
based, how can I position camera so player appears on left-hand side of screen when facing right and on the right-hand side when facing right?
The player can move left and right beyond the initial viewport, so when camera is following player id like the rule above to still be inforced.
Code below places player on left-hand side of screen when facing left, and when facing right the camera seems to remain still.
private OrthographicCamera camera = new OrthographicCamera();
camera.setToOrtho(false, 80, 48);
if (getPlayer().getDirection() == Direction.RIGHT){
camera.position.set(
new Vector3(camera.viewportWidth / 4, camera.position.y, camera.position.z));
}else{
camera.position.set(
new Vector3(camera.viewportWidth / 4 * 3, camera.position.y, camera.position.z));
}
Your current code fixes the camera to a certain point depending of which direction your character is moving. You need to lock the camera relative to player character.
Example when traversing right with little bit of pseudo code
camera.position.set(new Vector3(player.position.x + deltaForCamera.x, player.position.y + deltaForCamera.y, camera.position.z));
You can test what values are ideal for your scenario. Similar way works for when traversing on opposing direction, but you just need to figure the delta for player/camera relation.
Remember to do `camera.Update();
Cheers!