Camera 3D controller

Godot Version


I wrote a script for my camera(placed inside a pivot node 'Camera_Pivot) so the camera will follow the character from behind, like most third person games. However It just shows one direction. I;m unable to make the camera follow its rotation.

extends CharacterBody3D

@export var speed = 14
@export var fall_acceleration = 75
var character
var camera
@export var interval = 5.0
@export var height = 5.0
@onready var animationTree = $Pivot/Kun/AnimationTree

func _ready(): = true
camera = get_node(“Camera_Pivot”).get_global_transform()
character = get_node(“Pivot/Kun”)
const JUMP_VELOCITY = 40
#_process() allows you to update the node everyframe. delta is the time after each frame.
#_physice_process() is like _process() but for physics-related code like moving a kinematic or regid body

func _physics_process(delta):
#we create a local variable to store the input direction
var input:= Vector3.ZERO
#we check for each move input and update the direction accordingly
input.x = Input.get_axis(“move_left”,“move_right”)
input.z = Input.get_axis(“move_forward”, “move_back”)
translate(input * speed * delta)
if Input.is_action_just_pressed(“jump”) and is_on_floor():
#transform x and z is the same as player(kun) - distance offset
#transform y is same as += player height jump, maintaining camera height offset
var forward = $Camera_Pivot.transform.basis.z
if Input.is_action_pressed(“move_forward”):
forward = character.transform.basis.z
elif Input.is_action_pressed(“move_back”):
forward = -character.transform.basis.z
# get the global direction of the rigid body
var global_direction = global_transform.basis.z
# convert the global direction to local direction
var local_direction = to_local(global_direction)
#Vertical velocity
if not is_on_floor(): #if in air, fall towards the floor. Literally gravity
input.y = input.y - (fall_acceleration * delta)

update_animation_parameter() #calls this function
#rotate player
var char_rot = character.get_rotation()
if (animationTree["parameters/conditions/moving"] == true):
	var angle = atan2(-input.x, -input.z)
	char_rot.y = angle
#rotation.x and z is the same as player(kun)
$Camera_Pivot.transform.basis.rotated(Vector3(char_rot.x,char_rot.z,0), 360 )

move_and_slide() #allows character to move smoothly

func update_animation_parameter():
if not Input.is_action_pressed(“move_back”) and not Input.is_action_pressed(“move_forward”) and not Input.is_action_pressed(“move_left”) and not Input.is_action_pressed(“move_right”):
animationTree[“parameters/conditions/stopping”] = true
animationTree[“parameters/conditions/moving”] = false
animationTree[“parameters/conditions/moving”] = true
animationTree[“parameters/conditions/stopping”] = false

Check this property of Camera3D node: Ignore rotation

I don’t quite get you. Can you elaborate a bit further? I’m new to gdscript

[My previous answer is wrong]

%CamNode.transform = %CamNode.transform.rotated(Vector3.UP, PI / 90)