Rotating camera smoothly on button press

Godot Version

godot-4`

Question

Completely new programmer here - trying to get camera to rotate smoothly on Y-axis 18degrees to the left on Q button press. Been looking up lerps and tweens and completely losing my way.

Current code below allows for camera movement forwards, back, strafing left and right & to turn left or right abruptly by 18degrees.

Would love some help on how I interpolate(?) from current rotation on y axis to target rotation of 18degrees!

extends Camera3D

var move_speed: float = 5.0
var turn_left: float = 18.0
var turn_right: float = -18.0

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	var input_direction: Vector3 = Vector3.ZERO
		
	if Input.is_action_pressed("Forward"):
		input_direction.z -= 1.0
	if Input.is_action_pressed("Back"):
		input_direction.z += 1.0
	if Input.is_action_pressed("Left"):
		input_direction.x -= 1.0
	if Input.is_action_pressed("Right"):
		input_direction.x += 1.0
	if input_direction != Vector3.ZERO:
		input_direction = input_direction.normalized()
	translate(input_direction * move_speed * delta)

	if Input.is_action_just_pressed("Turn_left"):
		rotate_y(deg_to_rad(turn_left))
	if Input.is_action_just_pressed("Turn_right"):
		rotate_y(deg_to_rad(turn_right))

Try this:

rotation.y = lerp_angle(rotation.y, Input.get_axis("Turn_left", "Turn_right") * deg_to_rad(18), delta*7.5)

It`s too easy and simple, right? :sweat_smile:
Here, 7.5 is the speed of the rotation, and 18 is the tilt amount. Hope you can understand the code.
Or if you are trying to do camera tilt, then I think you need to change the rotation in z axis.

1 Like