Godot 3D Camera Clamp problem

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

So right now, I looked up tutorials as to how to clamp my camera so that it stops at certain positions. Unfortunately, I seem to hit yet another brick wall. I added a clamp function that I thought would do it but, the thing is that when I entered certain values in the x axis, the object is hugging the left corner of the screen, even if it’s possitioned to the center of the level. I only want it to do that when it’s actually on the left corner of the stage. Same with the camera clamping to the right, upper and lower areas

Here’s my code for anyone curious

extends Spatial

var isCameraFollowing = true
var player


func _ready():
	if get_tree().has_group("Player"):
		player = get_tree().get_nodes_in_group("Player")[0]
		
func _process(_delta):
	$InnerGimbal.translation.x = clamp($InnerGimbal.translation.x, 8, 8)
	$InnerGimbal.translation.z = clamp($InnerGimbal.translation.z, -5, -5)
	if isCameraFollowing == true:
		if player != null:
			global_translation = player.global_transform.origin
	else:
		if player != null:
			look_at(player.global_transform.origin,Vector3.UP)

if anyone can help a fellow Godot newbie out, I would appreciate it

:bust_in_silhouette: Reply From: BlotoPK

What are you trying to do in here:

$InnerGimbal.translation.x = clamp($InnerGimbal.translation.x, 8, 8) $InnerGimbal.translation.z = clamp($InnerGimbal.translation.z, -5, -5)

you have to use a min and a max value in the clamp, otherwise there is no range to clamp, just a assignment like:

$InnerGimbal.translation.x =8
$InnerGimbal.translation.z = -5

I imputed the code as it was written and it didn’t give me the results I wanted. Maybe this isn’t what I’m looking for

OmarBeltran | 2023-02-06 20:54

the way I see, your code only makes sense if you change the min and max of the clamp, just like this:

    if isCameraFollowing == true:
        if player != null:
            global_translation = player.global_transform.origin
    else:
        if player != null:
            look_at(player.global_transform.origin,Vector3.UP)

$InnerGimbal.translation.x = clamp($InnerGimbal.translation.x, -8, 8)
$InnerGimbal.translation.z = clamp($InnerGimbal.translation.z, -5, 5)

and I also put the clamp after the translation set.

BlotoPK | 2023-02-07 01:33

I tried this also but it made it so that the camera is fully focused on the player (which is good) but when I reach a corner, the camera doesn’t clamp at all

OmarBeltran | 2023-02-08 02:43

can be a lot of things… maybe the translation values never reach the clamp min and max, or maybe you’re clamping the translation of a wrong node? Maybe you’re having $InnerGimbal as a child of the player node?

BlotoPK | 2023-02-09 01:44

the innergimbal is a child of this one node called CameraGimbal if anyone’s curious
Also the camera is a child of the InnerGimbal

OmarBeltran | 2023-02-09 02:42