Help with the Limiting of My Camera2D

Godot Version

4.3.3

Question

Hello there, hope You’re having a great day today. Still working on my little Tower Defense game and I hit a small wall that bugged me out. Basically today I was experimenting and trying to create a building manager but I ran into some issues with my camera. The game is 2D and I want to have a camera for some of the larger levels that I want to create. So I created a Camera2D node and just added it to the Scene so I can just move left and right with the mouse.

extends Camera2D

var speed = 500

func _ready() -> void:
	pass
	
func _process(delta):
	var viewportSize = get_viewport().size
	var mousePos = get_viewport().get_mouse_position()
	if mousePos.x < 10:
		position.x -= speed * delta
	elif mousePos.x > viewportSize.x -10:
		position.x += speed * delta
	if mousePos.y < 10:
		position.y -= speed * delta
	elif mousePos.y > viewportSize.y -10:
		position.y += speed * delta

This is my camera code so far but it goes out of bounds since the first level is quite small. I tried to clamp it to the size of the screen that the level is on but on start the screen had only the left part of the map while the other part.
Will still google around to see if I can find a solution but wanted to post here just in hopes someone has an idea for this issue.

Camera2D has two property, limit and position_smoothing, adjust the settings to do this
Please read this properly
You not need to use clamp

Thank you so much, damn I always thought I had to go through it with code and tried to use clamp and what not, it worked but the camera was always focusing on weird parts of the screen. Didn’t think to check on the camera in the editor, that’s my bad, I completely missed those settings. Will try them out and experiment a bit with them now. Thanks again!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.