This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

I have a script that uses translate() to move the camera with the mouse, and the camera is limited to a specific area of the level but when the camera reaches those limits and if the player where to try to move the camera beyond the limits, the camera gets a bit stuck when trying to move away from the limits.
Here is a video of the issue, sorry about the weird size or the capture I don't know why it's doing that: https://imgur.com/a/d1pnLZA

Thanks in advance.

Godot version version 3.4
in Engine by (58 points)

1 Answer

+3 votes
Best answer

This is because you dont limit the cameras position,
the camera itself is still moving beyond the limits but the viewport isnt.
try something like this:

position = Vector2(clamp(position.x,limit_left+get_viewport_rect().size.x/2,limit_right-get_viewport_rect().size.x/2),clamp(position.y,limit_top+get_viewport_rect().size.y/2,limit_bottom-get_viewport_rect().size.y/2))

This will use the limits you gave the camera.

by (64 points)
selected by

Hey thanks for this. I'm a beginner and found this comment of yours and it's super helpful. Fixes my exact problem as well (the camera offset going past the limits). I'm struggling to understand your code though. I'm a beginner and it's just a lot. haha can you help break it down?

Thanks for the Reply, i am happy to help :)
I have broken the code down to make it more clear what is happening.
I also fixed an issue by now using global_position instead of position.

Use this code after you changed the position of the Camera2D
this code only works correctly when "Camera2D.anchor_mode" is Drag Center.

first we get the viewport size and divide it by 2 to get the viewport's center.

var viewport_half_x = get_viewport_rect().size.x/2
var viewport_half_y = get_viewport_rect().size.y/2

then we offset the limits to acount for the viewport size

var new_limit_left = limit_left+viewport_half_x
var new_limit_top = limit_top+viewport_half_y
var new_limit_right = limit_right-viewport_half_x
var new_limit_bottom = limit_bottom-viewport_half_y

lastly, we clamp the Camera2D's position between the new limits.

var new_x = clamp(global_position.x, new_limit_left,new_limit_right)
var new_y = clamp(global_position.y, new_limit_top,new_limit_bottom)

global_position = Vector2(new_x,new_y)

Final code written in an function:

func snap_in_limit():
    #this code only works correctly when "Camera2D.anchor_mode" is Drag Center.

    #first we get the viewport size and divide it by 2 to get the viewport's center.
    var viewport_half_x = get_viewport_rect().size.x/2
    var viewport_half_y = get_viewport_rect().size.y/2

    #we offset the limits to acount for the viewport size
    var new_limit_left = limit_left+viewport_half_x
    var new_limit_top = limit_top+viewport_half_y
    var new_limit_right = limit_right-viewport_half_x
    var new_limit_bottom = limit_bottom-viewport_half_y

    #clamp the Camera2D's position between the new limits.
    var new_x = clamp(global_position.x, new_limit_left,new_limit_right)
    var new_y = clamp(global_position.y, new_limit_top,new_limit_bottom)

    global_position = Vector2(new_x,new_y)

Feel free to ask if anything is unclear/not working properly.

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.