I am trying to create a 3rd person camera that can orbit around the character based on mouse input.
At the moment, I have managed to create a camera that rotates horizontally, but it pivots around itself instead of orbiting the character like I intended, even when I use the LookAt() method (using C#).
How can I achieve the intended functionality? This is my setup so far:
Node Setup
Player
--CameraRoot(Spatial) <--- CameraController.cs here
----horizontal(Spatial)
------vertical(Spatial)
--------ClippedCamera
Code
using Godot;
using System;
using static CustomMath.MathFunctions;
public class CameraController : Spatial
{
[Export]
private float mouseSensitivity = 10f;
private float cameraRotationHorizontal;
private Spatial camPivotH;
private Spatial camPivotV;
private float minAngle = 50f;
private float maxAngle = 50f;
public override void _Ready()
{
camPivotH = GetNode<Spatial>("h");
camPivotV = GetNode<Spatial>("h/v");
}
public override void _Input(InputEvent @inputEvent)
{
KinematicBody player = (KinematicBody)GetParent();
if (@inputEvent is InputEventMouseMotion mouseEvent)
{
camPivotH.RotateX(DegToRad(-mouseEvent.Relative.y * mouseSensitivity));
camPivotV.RotateY(DegToRad(-mouseEvent.Relative.x * mouseSensitivity));
Vector3 cameraRotation = camPivotH.RotationDegrees;
//cameraRotation.x = Clamp(cameraRotation.x, -minAngle, maxAngle);
camPivotH.RotationDegrees = cameraRotation;
camPivotH.LookAt(player.GlobalTransform.origin, Vector3.Up);
}
}
}