- Edited
So I've got a camera controller that I'm trying to move smoothly. I'm trying to get it such that I press the button to move, the camera gradually increases speed, the when the button is released, the camera gradually comes to a stop. I'd like to do the same for camera rotation.
The code for the camera is as follows:
@export_range(1.0, 10.0, 0.5) var move_speed: float = 5.0
@export_range(0.5, 3.0, 0.25) var rot_speed: float = 1.5
@export_range(0.5, 3.0, 0.25) var camera_smoothness: float = 0.5
var camera_can_move: bool = true
var camera_can_rotate: bool = true
func _process(delta) -> void:
move_camera(delta)
rotate_camera(delta)
func move_camera(delta) -> void:
if !camera_can_move: return
# Get desired movement based on input
var desired_movement = Vector3(
Input.get_axis("camera_left", "camera_right"),
0,
Input.get_axis("camera_forward", "camera_backward")
) * move_speed
# Current camera position
var current_position = global_transform.origin
# Lerp between current and desired position with delta for smoothness
var new_position = lerp(current_position, current_position + desired_movement, delta * camera_smoothness)
# Update camera position
translate(new_position - current_position)
func rotate_camera(delta) -> void:
if !camera_can_rotate: return
# Get desired rotation based on input
var desired_rotation = Vector3(0, Input.get_axis("camera_cw", "camera_ccw"), 0) * rot_speed
# Current camera rotation (assuming rotation around Y-axis)
var current_rotation = rotation.y
# Lerp between current and desired rotation with delta for smoothness
var new_rotation = lerp(current_rotation, current_rotation + desired_rotation.y, delta * camera_smoothness)
# Update camera rotation (assuming rotation around Y-axis)
rotate_y(new_rotation - current_rotation)