How to cap camera movement based on distance from player

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

HI All, I need some ideas. I have a RTS type setting featuring the character node with a high-angle camera looking at it. I can move and rotate the camera independent of the character to survey the map in its vicinity (using WASD and mouse). I want to stop the camera from moving too far away from the character node (to prevent the player from flying over the whole map without the character even going anywhere).

I tried to do this using distance_to (with a cam movement speed variable being set to 0 once a certain distance is reached) which does stop the camera movement but also prevents me from WASD-ing back to the character because the max distance condition is still met and cam movement speed is still 0.

Perhaps someone can think of a better approach?

:bust_in_silhouette: Reply From: DDoop

Might be a good use for the built-in clamp(value, min, max) function, called once per frame on a variable that keeps track of the camera’s positional offset. It takes floats, though, so I’m not sure what the specific implementation would be.