Hi, I’m learning how to use quaternion so I’m trying to create a simple smooth third person camera with it. The camera setup consists of a Camera3D node inside a CameraPivot node. The rotation works fine but when I use slerp to smooth the camera, it jitters horizontally, moving back and forth between left and right.
using Godot;
public partial class CameraController : Node3D
{
[Export] public float yawSensitivity = 0.07f;
[Export] public float pitchSensitivity = 0.07f;
[Export] public float smoothSpeed = 10f;
private float yaw = 0f;
private float pitch = 0f;
private Basis targetRotation = Basis.Identity;
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Captured;
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event is InputEventMouseMotion mouseMotion && Input.MouseMode == Input.MouseModeEnum.Captured)
{
yaw += mouseMotion.Relative.X * yawSensitivity;
pitch -= mouseMotion.Relative.Y * pitchSensitivity;
pitch = Mathf.Clamp(pitch, Mathf.DegToRad(-70), Mathf.DegToRad(70));
Quaternion yawQuat = new Quaternion(Vector3.Up, yaw);
Quaternion pitchQuat = new Quaternion(Vector3.Right, pitch);
targetRotation = new Basis(yawQuat * pitchQuat);
}
}
public override void _Process(double delta)
{
//targetRotation = Basis.Slerp(targetRotation, (float)(smoothSpeed * delta));
Basis = targetRotation;
}
}