0 votes

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);
        }
    }
}
Godot version 3.4.4 mono
in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.