Godot Version
4.3
Question
How do i clamp a camera 2d Node?
i have a camera 2d that moves on the x axis (and x axis only), how would i go and use a clamp function to stop it
4.3
How do i clamp a camera 2d Node?
i have a camera 2d that moves on the x axis (and x axis only), how would i go and use a clamp function to stop it
Can you give me the code used to move it?
extends Camera2D
var isHoveringRight = false
var isHoveringLeft = false
func _process(_delta):
if isHoveringRight == true:
position.x += 10
if isHoveringLeft == true:
position.x -= 10
var xclamp = 2403
position.clamp() #thing i need
func _on_navigate_right_body_entered(_body):
isHoveringRight = true
func _on_navigate_right_body_exited(_body):
isHoveringRight = false
func _on_navigate_left_body_entered(body):
isHoveringLeft = true
func _on_navigate_left_body_exited(body):
isHoveringLeft = false
camera.position.x = clamp(camera.position.x, deg_to_rad(-100), deg_to_rad(100))
That might work but I have not tested it and you may need to remove the camera in camera.position depending on how itâs set up
also change the 100 to whatever you want
(I forgot to add .x to the position as pointed out by the other reply)
I believe you would use position.x.clamp(mapsize min,mapsizemax)
So if your map was say 10000 pixels long, you should be able to do (0, 10000).
If say you run into an issue where itâs clamping but it over extends the boundary you want, subtract half of the camera width from the 10000 and that should do it.
It says "Unexpected identifier in class body :(.
âCannot find property âclampâ on base âfloatââ
something about camera not being declared in the current scope
Edit: so i went and fixed the not declared problem
but know its spits out that position is a null instanse
I am trying to replicate your camera in a new project. Can you show me the scene tree
that button there is not related to the problem im trying to fix
actually never mind about the reply I deleted this is a much better way to use clamp() that I just discovered
position.x = clamp(position.x,deg_to_rad(-xclamp),deg_to_rad(xclamp))
you may want to change the value of xclamp
someone pointed out that Camera2D has built in Limits you can change
You already have camera limit properties.
You can set them up in your scene or if theyâre dynamic you can set them via code based on your current map size.
there goes the ui