0 votes

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

Godot version 3.5.1
in Engine by (17 points)

1 Answer

0 votes

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

by (75 points)

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

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.

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

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?

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.