I'm trying to make a 2D star map, and I've managed to add a zooming feature and allow it to move. But I can't figure out how to prevent the camera from going too far in all directions (Up, down, left, right.) Any help would be greatly appreciated!
Borders for moving a Camera2D
- Edited
imo restricting camera's x y should do the trick, e. g:
func _process():
var camera = get_viewpoint(). get_camera()
if camera.position.x > MAP_MAX_X– CAMERA_RANGE_X:
camera.position.x = MAP_MAX_X– CAMERA_RANGE_X:
if camera.position.x < MAP_MIN_X + CAMERA_RANGE_X:
camera.position.x = MAP_MIN_X + CAMERA_RANGE_X
MagickPanda Thanks for the suggestion! I have copied the code down and changed the values to what I need them to be; however I am now getting an error saying that, "The method 'get_viewpoint' isn't declared in the current class." I'm not entirely sure what that means.
And another question, does the code need to be in the Camera node or the scene node?
Thanks again!
- Edited
TysonIsGud You just need to directly get self.position if you attach the script to the camera itself.
Get_view point is used when you attach script to scene root node.
So, I managed to get there to be (so it says) no errors with my code. However, when I launch my scene, I am still able to move past my set coordinates. I'm not sure why, as there are no errors. Thank you so much for your help so far!
TysonIsGud np. just paste the problematic code here, so others might be able to help you pinpoint the problem.
So this is my code; it's in the Camera2D node.
func _edge():
var camera = self.position
if camera.x > 1000 - 1500:
camera.x = 1000 - 1500
if camera.x < 0 + 1500:
camera.x = 0 + 1500
if camera.y > 1000 - 870:
camera.y = 1000 - 870
if camera.y < 0 + 870:
camera.y = 0+ 870
- Edited
imo it should be
# A 2048*1024 area
const BOUND_X_MIN = -1024
const BOUND_X_MAX = 1024
const BOUND_Y_MIN = -640
const BOUND_Y_MAX = 640
# Camera view range limits is 1000*1000(500 from camera center)
const CAMERA_LIMIT_X = 500
const CAMERA_LIMIT_Y = 500
#Get min and max bounds
const MIN_X = BOUND_X_MIN + CAMERA_LIMIT_X
const MIN_Y = BOUND_Y_MIN + CAMERA_LIMIT_Y
const MAX_X = BOUND_X_MAX - CAMERA_LIMIT_X
const MAX_Y = BOUND_Y_MAX - CAMERA_LIMIT_Y
if camera.x < MIN_X:
camera.x = MIN_X
if camera.x > MAX_X:
camera.x = MAX_X
if camera.y < MIN_Y:
camera.y = MIN_Y
if camera.y > MAX_Y:
camera.y = MAX_Y
p.s:fixed order of declaration
It's easier to use ~~~ before and after.
duane Thanks for the fix and hint.
Alright I replaced the code and with what you gave me, and I'm not getting a "Error parsing expression, misplaced: const" error. Not sure what this means. FYI I put this in my function, I would think that's where it needs to go, but if not please let me know. Thanks!
TysonIsGud You stil need to replace width and height(2048*2048) with your boundary limits, and camera ortho viewpoint w and h with your desired values if you want to use the code I pasted.