Topic was automatically imported from the old Question2Answer platform.
Asked By
Sk1ppeR
I’m working on a game that lets the player play as a bird. It has a parallax background and a designed level with obstacles and enemies. Currently the player is allowed to “go backwards”. I figured I can prevent that by dynamically setting the camera limits, but nothing stops the player from going out of camera boundaries. And on second thought it was pretty silly to use the camera as a boundary. Thing is I can’t come up with a solution ala supermario where you are only allowed to go forward through the stage and never go backwards. How would you implement such a movement strategy? I was thinking if putting some colliding object at the beginning of the screen but given the bird can go as high as the player wisher and as low as the player wishes, isn’t it a bit risky?
What about attaching a collision to the left of you camera? You follow the player only when he’s in the right side of the screen. When the player goes up, down or right the camera follow, and when he wants to go left, the camera stop following and he’ll collide.
If this is in 2D and your char and camera moves when your char is center of your camera
#get the viewport size and divide by 2 since this is where the camera is positioned
var view = get_viewport_rect().size / 2
#get the camera position
var camera_pos = $Camera2D.global_position
var bounds_bw = camera_pos.x - view.x #the camera bounds at the back
var bounds_fw = camera_pos.x + view.x #the camera bounds at the front
#after the character is moved clamp its position to the end of the camera bounds
$Bird.global_position.x = clamp($Bird.global_position.x, bounds_bw, bounds_fw)
I am using move and slide my player can’t move in x axis
Codell Musukwa | 2022-12-20 13:32
After the character is moved clamp its position to the end of the camera bounds
var bounds_uw = camera_pos.y - view.y #the camera bounds at the top
var bounds_dw = camera_pos.y + view.y #the camera bounds at the bottom
$Bird.move_and_slide(velocity, Vector2.UP)
$Bird.global_position.y = clamp($Bird.global_position.y, bounds_uw, bounds_dw)