0 votes

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?

Godot version 3.2.3-stable
in Engine by (17 points)

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.

2 Answers

+1 vote
Best answer

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)
by (4,698 points)
selected by
0 votes

I think I would just clamp() players position x and y between defined max and min level borders, in players movement code

by (3,336 points)

How do you clamp when we're using moveandslide() or moveandcollide() ?

If You use those, than just additional line in process() about global_position, just like in the answer above

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.