Hi, first time using Godot with very little programming knowledge.

I'm trying to set up limits for a Camera2D that can be moved with input keys. The inspector lets you set up limits, but those limits only affect the frame of the camera, not the actual Camera node that keeps moving beyond those limits, creating a delay when i turn back the other way (the camera frame doesn't move until the node moves back inside the limits, only then it moves again)

So i wrote very basic lines to force positions, but strangely it's only working on the X axis, not on Y. The Y position seems to be all wrong from the start for some reason.
Can someone help me figure out why this is happening?
Thanks!

`func _process(delta):
Input.get_vector("Left","Right","Up","Down")

if Input.is_action_pressed("Up"):
		position+=Vector2(0,-500)*delta
if Input.is_action_pressed("Down"):
		position+=Vector2(0,500)*delta
if Input.is_action_pressed("Left"):
		position+=Vector2(-500,0)*delta
if Input.is_action_pressed("Right"):
		position+=Vector2(500,0)*delta
if position < Vector2(187,0):
	position.x = (187)
if position > Vector2(953,0):  		
	position.x = (953)
     if position <  Vector2(0,107):
	position.y = (107)
if position > Vector2(0,530):
	position.y = (530)`
  • xyz replied to this.

    Gastongeg Compare only individual (x or y) components, not the whole vectors. Or better yet use the clamp() function.

      xyz

      Thanks, it was so simple, so the error was mixing the whole vector.

      I'm not familiar with the clamp function. I read that it needs 3 values. The max and min of each axis i already got, but don't know what should be the variant value?
      Thanks!

      • xyz replied to this.

        Gastongeg but don't know what should be the variant value

        The value you want to clamp.

          5 days later

          xyz
          I just want to set a minimum and maximum for x and y. From 187 to 953 on X and 107 to 530 on Y.
          I don't care for values in between, just to don't go over the min and max.
          Does clamp() helps with that?

          • xyz replied to this.

            Gastongeg Have you read the description of clamp() in the docs, on the link I posted above?

            16 days later

            Sorry for not replying, i work on this project on my free time and sometimes i left it unchecked for long periods.

            But i figured it out, thanks a lot for your help!

            position.x = clamp(position.x, 187, 953)
            position.y= clamp(position.y, 107, 530)