Issue Camera Limit

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Neestrid

Hello,

I am currently using GoDot 4. And I would like to move my camera with WSAD but I have an issue with the limit.

My view is stopping at the limit I fixed in the parameters (2000x2000).

However, the value’s limit goes further than it should. I don’t know what value should I put as the limit because it depends on the Zoom. Which causes the value to increase when the view reached his limits.I can’t find any solution to my problem. This issue increase as I zoom out.

I think it has to do with the fact that the camera’s position is at the center.

Here’s a video of my problem : https://streamable.com/hhpzwy

My code :

extends Camera2D

const MIN_ZOOM = Vector2(0.5, 0.5)
const MAX_ZOOM = Vector2(4, 4)
var LIMIT = 2000

# Called when the node enters the scene tree for the first time.
func _ready():
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	print("Cam Position = ",position)
	zoom()
	deplacement()


func zoom():
	print("Zoom = ", zoom)
	if Input.is_action_just_released("ZoomOut") and get_zoom() > MIN_ZOOM:
		set_zoom(get_zoom() - Vector2(0.25, 0.25))
	if Input.is_action_just_released("ZoomIn") and get_zoom() < MAX_ZOOM:
		set_zoom(get_zoom() + Vector2(0.25, 0.25))
	
func deplacement():
	if Input.is_action_pressed("up") and position.y > -LIMIT:
		position.y -= 3
	if Input.is_action_pressed("down") and position.y < LIMIT:
		position.y += 3
	if Input.is_action_pressed("right") and position.x < LIMIT:
		position.x += 3
	if Input.is_action_pressed("left") and position.x > -LIMIT:
		position.x -= 3

Thank you!

:bust_in_silhouette: Reply From: Neestrid

I managed to fix it :

    extends Camera2D
    
    const MIN_ZOOM = Vector2(0.5, 0.5)
    const MAX_ZOOM = Vector2(4, 4)
    var LIMIT = 2000
    # Called when the node enters the scene tree for the first time.
    func _ready():
    	var width = Vector2.ZERO
    	var wide = Vector2.ZERO
    
    
    # Called every frame. 'delta' is the elapsed time since the previous frame.
    func _process(delta):

       #FIX
    	var width = DisplayServer.window_get_size().x
    	var wide = DisplayServer.window_get_size().y
    	
    	width = width / get_zoom().x
    	wide = wide / get_zoom().y
    
    	print("Cam Position = ",position)
    	zoom()
    	deplacement(width, wide)
    
    
    func zoom():
    	print("Zoom = ", zoom)
    	if Input.is_action_just_released("ZoomOut") and get_zoom() > MIN_ZOOM:
    		set_zoom(get_zoom() - Vector2(0.25, 0.25))
    	if Input.is_action_just_released("ZoomIn") and get_zoom() < MAX_ZOOM:
    		set_zoom(get_zoom() + Vector2(0.25, 0.25))
    	
   #UPDATE THE CONDITION
    func deplacement(width, wide):
    	#print(position)
    	if Input.is_action_pressed("up") and position.y > -LIMIT + (wide/2):
    		position.y -= 3
    	if Input.is_action_pressed("down") and position.y < LIMIT - (wide/2):
    		position.y += 3
    	if Input.is_action_pressed("right") and position.x < LIMIT - (width/2):
    		position.x += 3
    	if Input.is_action_pressed("left") and position.x > -LIMIT + (width/2):
    		position.x -= 3

30 minutes break are so powerful