- Edited
I have a camera which is a child of an empty object3D (called Pivot) which has a script attached so that it rotates on mouse drag. I'm just using the x coordinate of mouse click events to update the rotation of the Pivot. The rotation also lerps to a certain snapping angle (set to 90 degrees in the example). Since I'm pretty new to handling rotations and conceptualising how the position of the mouse can be transformed into a useful vector for rotation, I've gotten stuck on an issue where occasionally my rotation stalls for a second, and bounces in the other direction? Could anyone help explain why this is happening?
Here's my scene setup and code:
class_name Pivot extends Node3D
@onready var camera : Camera3D = $Camera3D
@export var snapping_angle : int = 45
@export var mouse_sensitivity : float = 2
@export var rotation_speed : float = 5
@export var current_angle : float
@export var dragging = false
var target_angle : float = 0
func _process(delta):
current_angle = lerp_angle(rotation.y, target_angle, rotation_speed * delta)
rotation.y = current_angle
func _input(event : InputEvent):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if not dragging and event.is_pressed():
dragging = true
if dragging and not event.is_pressed():
dragging = false
if event is InputEventMouseMotion and dragging:
var angle = event.position.x * mouse_sensitivity
_update_target_angle(angle)
if event.is_action_pressed("ui_left"):
_update_target_angle(target_angle - snapping_angle)
if event.is_action_pressed("ui_right"):
_update_target_angle(target_angle + snapping_angle)
func _update_target_angle(angle : float):
angle = roundi(angle / snapping_angle) * snapping_angle
target_angle = deg_to_rad(-angle)