Camera Directional Movemet?

Godot Version 4

Question

So I have watched about 4 tutorials and the code has been pretty much the exact same, but when I use it It dosen’t work. When I look left, forward should be left and ect… But left, right, backwards, and forwards stay the same no matter where I look. I will not check until Tmr. The following is my code:

extends CharacterBody3D



func _input(event):
	if event is InputEventMouseMotion:
		
		if Input.mouse_mode != Input.MOUSE_MODE_CAPTURED: return
		%head.rotate_y(-event.relative.x * SENS)
		%Camera3D.rotate_x(-event.relative.y * SENS)
		%Camera3D.rotation.x = clampf(%Camera3D.rotation.x, -PI/2.0, PI/2.0)

func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()

func _ready():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

# variables
var _mouse_input : bool = false
var _mouse_rotation : Vector3
var _rotation_input : float
var _tilt_input : float
var _player_rotation : Vector3
var _camera_rotation : Vector3
var t_bob = 0.0

# @export variables
@export var TILT_LOWER_LIMIT := deg_to_rad(-90.0)
@export var TILT_UPPER_LIMIT := deg_to_rad(90.0)
@export var CAMERA_CONTROLLER : Camera3D
@export var MOUSE_SENSITIVITY : float = 0.5 

# @onready variables
@onready var head = $head
@onready var camera = $head/Camera3D

# constant 
const BOB_FREQ = 2.0
const BOB_AMP = 0.08
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
const SENS : float = 0.001

func _unhandled_input(event):
	_mouse_input = event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
	if _mouse_input :
		_rotation_input = -event.relative.x * MOUSE_SENSITIVITY
		_tilt_input = -event.relative.y * MOUSE_SENSITIVITY
		print(Vector2(_rotation_input,_tilt_input))

func _update_camera(delta):
	
	_mouse_rotation.x += _tilt_input * delta
	_mouse_rotation.x = clamp(_mouse_rotation.x, TILT_UPPER_LIMIT, TILT_UPPER_LIMIT)
	_mouse_rotation.y += _rotation_input * delta
	
	_player_rotation = Vector3(0.0,_mouse_rotation.y,0.0)
	_camera_rotation = Vector3(_mouse_rotation.x,0.0,0.0)
	
	
	CAMERA_CONTROLLER.transform.basis = Basis.from_euler(_camera_rotation)
	CAMERA_CONTROLLER.rotation.z = 0.0
	
	global_transform.basis = Basis.from_euler(_player_rotation)
	
	_rotation_input = 0.0
	_tilt_input = 0.0
	
	var direction = (head.transform.basis * Vector3(TILT_UPPER_LIMIT, 0, TILT_UPPER_LIMIT)).normalize
	
	#head bobbing(It dosen't work!)
	t_bob += delta * velocity.length() * float(is_on_floor())
	camera.transform.origin = _headbob(t_bob)
	
func _headbob(time) -> Vector3:
	var pos = Vector3.ZERO
	pos.y = sin(time * BOB_FREQ) * BOB_AMP
	return pos

This seems like your best implementation. Maybe change it to _unhandled_input and remove your current _unhandled_input function.

Your template 3D basic movement uses the CharacterBody3D’s transform.basis to change control direction, so you need to rotate_y the player instead of the “head” to change input directions.

Your _update_camera function is never called. I would remove it.