Movement rotation Help

Godot Version

Stable 4.6

Question

I cant get the movement direction to rotate when I turn the camera. Can someone point me in the right direction?

extends CharacterBody3D

# @onready var _camera := $Target/Camera3D as Camera3D

@onready var _camera = $CameraPivot/SpringArm3D/Camera3D 
@onready var _camera_pivot = $CameraPivot

const SPEED = 5.0
const JUMP_VELOCITY = 4.5


func _physics_process(delta: float) -> void:
	# 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

	# unLock camera
	if Input.is_action_just_pressed("ui_cancel"):
		if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

	# Lock camera
	if Input.is_action_just_pressed("LMC"):
		if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

	# 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("ui_left", "ui_right", "ui_up", "ui_down")
	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()
@export_range(0.0, 1.0) var mouse_sensitivity = 0.01
@export var tilt_limit = deg_to_rad(75)


func _unhandled_input(event: InputEvent) -> void:
	# Mouselook implemented using `screen_relative` for resolution-independent sensitivity.
	if event is InputEventMouseMotion:
		if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
			_camera_pivot.rotation.x -= event.screen_relative.y * mouse_sensitivity
			# Prevent the camera from rotating too far up or down.
			_camera_pivot.rotation.x = clampf(_camera_pivot.rotation.x, -tilt_limit, tilt_limit)
			_camera_pivot.rotation.y += -event.screen_relative.x * mouse_sensitivity

var direction := (_camera.transform.basis

try that

You need to add a type your camera, it’s a good idea to type any variable you can

@onready var _camera: Camera3D = $CameraPivot/SpringArm3D/Camera3D

didn’t work. still works the exact same as before

How does it not work? For anyone to help they must know what you expected to happen versus what really happens, “doesn’t work” isn’t enough information

Yeah camera based movement really just needs the transform basis to be off the camera, it’s a common way to solve that particular issue. I’ve seen (and used) this code before (off a YT tutorial that’s popular with newcomers). If camera.transform.basis isn’t working, can you provide a video example of the issue? It might be a separate issue.

It should be global_basis though. The resulting vector may need additional projection onto the horizontal plane, typically done by Vector3::slide()