0 votes

I have an overhead top down camera setup for my game and i use the mouse to scroll it, but when the camera reaches its set limits (left : -1000, Top : -500, right: 3000, bottom : 2500) it goes beyond it but the viewport doesn't, how do i clamp the camera to only move within the set limits and not beyond, thanks in advance! (camera code below)

    extends Camera2D

var is_panning = true;
var cam_spd = 10

onready var editor = get_node("/root/World/Cam_container")
onready var editor_cam = editor.get_node("Camera2D")
onready var maptype = get_node("/root/World/Compositemaps").maptype

func _positioner():
    if maptype == 'dungeonmap1':
             position = Vector2(152,64)
    elif maptype == 'dungeonmap2':
             position = Vector2(1592,1360)
    elif maptype == 'dungeonmap3':
             position = Vector2(1680,144)
    elif maptype == 'dungeonmap4':
            position = Vector2(1432,1576)

func _ready():
    editor_cam.current = true
func _process(_delta):

    is_panning = Input.is_action_pressed("mb_left")

func move_editor():
    if Input.is_action_pressed("w"):
        editor.global_position.y -= cam_spd
    if Input.is_action_pressed("a"):
        editor.global_position.x -= cam_spd
    if Input.is_action_pressed("s"):
        editor.global_position.y += cam_spd
    if Input.is_action_pressed("d"):
        editor.global_position.x += cam_spd

func _unhandled_input(event):
    if(event is InputEventMouseButton):
            if(event.button_index == BUTTON_WHEEL_UP):
                editor_cam.zoom += Vector2(0.1,0.1)
            if(event.button_index == BUTTON_WHEEL_DOWN):
                editor_cam.zoom += Vector2(0.1,0.1)
    if(event is InputEventMouseMotion):
            editor.global_position -= event.relative * editor_cam.zoom

video of what happens : https://streamable.com/t24jby

Godot version v3.3.4.stable.official
in Engine by (23 points)

1 Answer

0 votes

I'm a little confused trying to guess what the scene hierarchy you are working with looks like. Is the object attached to the script you have provided the main camera?

To me it looks like you need to clamp the individual components of the global_position inside move_editor() after the global_position vector is modified. I would replace pass with:
editor.global_position.x = clamp(editor.global_position.x, -1000, 3000) editor.global_position.y = clamp(editor.global_position.y, -500, 2500)

My apologies if I am misunderstanding what exact object you are trying to fix inside the given area but clamp()'ing the vector components is how (provided you can then assign back into the same variable which you should be able to here). The clamp() call needs to be done after whatever movement the object performs that potentially could take it out of the desired area (one could also cause clamp() to fire conditionally if performance became an issue later but don't try setting that up unless it becomes necessary).

by (739 points)

That worked, thank you so much!

is there a way to clamp the zoom too ?

nvm made it work too, thanks!

Clamp will always return either the provided value, the minimum, or the maximum. Inside _unhandled_input() after both conditional blocks that modify the zoom property, just add something like:
zoom = clamp(zoom, min, max)
You'll need to specify the min and max, though.

A note on your code overall: Is there a reason that you refer to zoom and global_position via a local instance variable? If you remove all the editor_cam references, does it still behave the way you want? Since your script extends Camera I have a feeling you are going through too much work to get a reference to self, but I can imagine reasons to make it like that.

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 Frequently asked questions and 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 [email protected] with your username.