Search code examples
javalibgdxbox2d

Making a body move along with the camera


I'm currently making a basic 2d platformer in libGdx, and I want to check when the player isn't within the camera boundaries and, if so, I want to make him restart the level.

The camera moves constantly at the speed of 0.5 y coordinates per tick, so will have to use setLinearVelocity on a kinematic body or is there another way?


Solution

  • Here is one Solution

    You can use

    camera.position.x 
    

    to get the current position of your camera. Then you compare the camera position to the character's position

    if(camera.position.x - distance_left_From_The_Center_of_Your_Screen < character.position) {
    camera.translate(-0.5, 0)
    }
    

    And this should move your camera