Godot Version
v4.3
Question
I’m trying to recreate the camera from older Zelda titles where the camera will follow the player while always keeping the player center screen. I have it set up in the world scene (not in the player controller) so that the camera looks at the player and follows whenever the player gets too far away. I’d like to be able to keep the camera a minimum distance away too, I’m just not sure what to code.
Here is the camera_rig code:
Here is the player code:
Specifically, I am trying to imitate windwaker’s camera settings. The camera will work independently of the right joystick (or mouse) until it is used and then enter “free camera” mode so that the player can manually move the camera. When in “automatic mode” the camera will pan to keep the player in frame and can even do things like pan down when standing near a ledge so that the player can see what is below. I am hoping to recreate that.
3D cameras like this can be a difficult problem; the simple cases are easy, but you need to think about things like whether other objects in the scene are going to obscure the player, whether player movement during combat will jerk the camera around too much…
For your camera script, I’d suggest starting with something like:
const MAX_DIST = 3.0
const MIN_DIST = 1.0
const MOVE_COEFF = 0.5 # What fraction of the distance do we move?
func _process(delta):
var vec = player.global_position - global_position
vec.z = 0.0 # Zero out the height difference.
var len = vec.length()
if len < 0.001: # We're way too close, fudge the math...
vec = Vector3.FORWARD
if len > MAX_DIST:
position += vec * MOVE_COEFF * delta
elif len < MIN_DIST:
position -= vec * MOVE_COEFF * delta
That will move the camera faster the further it is out of position.
I like your approach because the variables/constants definitely keep the code cleaner and zeroing out the height difference makes sense (I was having to manually adjust for height while working on the issue myself) but I’m starting to think I might have to find a new way without the use of +=, -=, or lerp. The move coefficient is either too low and allows the camera to lag really far behind, or its too high and the camera gets shaky. Your code does correctly move the camera away from the player if it gets too close and I thank you for pointing me in the right direction, but the MIN_DIST can be easily overcome by the player walking straight to the camera even at high move coefficients. I’m going to keep tinkering with it, thank you for your help.