I want the main menu background to move around when I move my mouse so that user can see more of the background if they move the cursor to the left, top, bottom or right. The background image is a bit larger than the game’s viewport.
The way I’m handling it now is resulting in the game showing more than the height and width of my background image is, showing godot’s grey background etc. There’s seems to be no limit.
How can I do this better to achieve what I want and only allow the user to see the whole background but not go beyond it? I’m guessing maybe move the background image itself, instead of the center of viewport? Here’s my code:
@onready var node = $Control
var center: Vector2
func _ready():
center = Vector2(get_viewport_rect().size.x/2, get_viewport_rect().size.y/2)
func _process(_delta):
var tween = node.create_tween()
var offset = center - get_global_mouse_position() * 0.1
But I found out that camera limits are only “visual”, they restrict what you see but they don’t restrict camera position. Meaning if I move my mouse to the left and hit the left limit, the image stays still but the camera continues drifting left. Then when I move right, there is a noticeable delay because the camera is somewhere off-screen, and it has to come back first before it can visibly move to the right. (relevant GitHub issue here: Camera2D limits don't clamp camera position · Issue #62441 · godotengine/godot · GitHub)
I fixed this by clamping the position of the camera instead of using limits, like this: