Godot Version
4.3
Question
Hi everyone,
I’m implementing a first-person controller in Godot 4.3 and having issues with my camera smoothing logic when walking up stairs. While this works perfectly in my demo project, it doesn’t function correctly in my main project. Here’s the node structure, relevant code, how the logic is supposed to work and the problem I’m encountering:
var _saved_camera_global_pos = null
func _save_camera_pos_for_smoothing() -> void:
if _saved_camera_global_pos == null:
_saved_camera_global_pos = %StairCameraSmooth.global_position
print("_saved_camera_global_pos: ", _saved_camera_global_pos)
func _slide_camera_smooth_back_to_origin(delta: float) -> void:
if _saved_camera_global_pos == null: return
%StairCameraSmooth.global_position.y = _saved_camera_global_pos.y
%StairCameraSmooth.position.y = clamp(%StairCameraSmooth.position.y, -0.7, 0.7) # clamp incase of teleport
var move_amount = max(self.velocity.length() * delta, 3 * delta)
%StairCameraSmooth.position.y = move_toward(%StairCameraSmooth.position.y, 0, move_amount)
_saved_camera_global_pos = %StairCameraSmooth.global_position
if %StairCameraSmooth.position.y == 0:
_saved_camera_global_pos = null # stop camera smoothing
Logic for Walking Up a Step:
Saving the Camera Position:
When the player walks up a step, I use a raycast to detect the step and adjust the player’s position (CharacterBody3D) to “snap” to the top of the step. Right before moving the player, I save the StairCameraSmooth
node’s global_position (_saved_camera_global_pos
) for smoothing.
Camera Adjustment:
After snapping the player to the top of the step, the StairCameraSmooth node should move smoothly back to its original position relative to the parent node (CameraController
).
To achieve this, I set %StairCameraSmooth.global_position.y
to _saved_camera_global_pos.y
, this creates an offset between the CameraController
and StairCameraSmooth
global positions, resulting in a non-zero local position: %StairCameraSmooth.position.y != 0
.
Smoothing the Camera:
The local position.y of %StairCameraSmooth
is gradually moved back to 0 using move_toward
. This creates the visual effect of the camera following the player’s movement smoothly, instead of snapping instantly.
Expected Behavior:
When stepping up:
The child node’s global position (%StairCameraSmooth.global_position.y) is set to _saved_camera_global_pos.y, which is slightly lower than the player’s (and therefore CameraController
’s) new position.
This creates a non-zero local position.y (%StairCameraSmooth.position.y
), which is smoothed back to 0 over time.
The smoothing logic works perfectly in my demo project, producing a seamless transition.
Problem in the Main Project:
When moving up a step, the parent (CameraController
) and child (StairCameraSmooth
) always have identical global_position.y values.
As a result, the local position.y
of %StairCameraSmooth
is always 0, so the smoothing logic (move_toward
) has no effect.
I have been looking for the error for hours now and would appreciate any help greatly. If you need any more information please ask.
Thanks in advance for any replies