I need help with a 3d camera and its relative movement

Godot Version

Godot 4.6.2.stable

Question

I’ve tried multiple times to make a camera with relative movement, but I can’t make anything work, I just wanted to know if anyone has any suggestions/ tutorials, I’m willing to change versions if it helps

If you share code, please wrap it inside three backticks or replace the code in the next block:

func _ready() -> void:
    print("hello world")

What’s “relative movement”? Relative to what?

to the camera, I want to turn a 3rd person camera and still move forward so if I turn the camera completely around, I want the player to turn around and move in the new forward direction

You want player movement to be relative to camera’s viewing direction? Project the input directions to x and z axes and then project the result onto horizontal plane.

Two things to keep in mind:

  • You track your camera independent to the player
  • The player can access the camera.

So, to allow the player to face the model toward the camera direction this snippets could help:

	# Get the input direction and handle the movement/deceleration.
	var direction = Vector3(input_dir.x, 0, input_dir.y).rotated(Vector3.UP, _camera_mount.rotation.y)

I’m sorry I still don’t understand, should I show my camera script? the most common issue is the line doesn’t recognize “_camera”

You’re scene has a camera so you need to retrieve it. usually on ready but you can set it manually too.
Auto

@onready var camera : Camera3D = camera_mount.get_node("Camera3D")

or

var camera: Camera3D = get_viewport().get_camera_3d()

Manual

var _camera_mount : Node3D

Depends on your implementation

ive done that with

@onready var camera: Camera3D = $SpringArmPivot/camera

did I write it wrong?

Post your scene setup.

Ive added ### to unused code

extends CharacterBody3D

const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@onready var camera: Camera3D = $SpringArmPivot/camera
###@onready var camera : Camera3D = camera_mount.get_node("Camera3D")
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("jump") 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("left", "right", "forward", "backward")
	
	# new vec 3 acounting for arrow imputs and cam rotation
	var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	###direction = direction.rotated(Vector3.FORWARD, camera.global_rotation.y)
	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()

If I read properly without executing the code I will say you are moving based on the original direction to front and sides. Not aligned with the camera. Only based on the original setup of the CharacterBody3D of the Godot template code.

Here’s some boilerplate code that can handle moving a CharacterBody3D with respect to the direction that the camera is facing.

func handle_move() -> void:
	var move: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
	var direction: Vector3 = Vector3.ZERO
	if move != Vector2.ZERO:
		direction = Camera.basis * Vector3(move.x, 0, move.y)
		direction = Vector3(direction.x, 0, direction.z).normalized();

	velocity.x = direction.x * speed
	velocity.z = direction.z * speed

	move_and_slide()

Here’s what’s happening, at a high level. We first have an input source, gathered from our actions moving in the four directions forward, back, left, and right. Together, that’s a 2D vector that is “flat” along the X-Z plane. That vector defines the direction that we want to move in.

Next we need to apply the camera’s basis to the direction that we have, so we multiply the basis by the desired direction. This gives us the vector relative to the Camera’s rotation, which is what we want here. Next, we zero out the y value (because we don’t want to move up or down) and normalize the vector.

Then we can adjust the velocity of the body with respect to the desired speed, and then call move_and_slide()

I added this under the movement code and it has an issue, It says “Invalid operands “Vector2” and “Vector3” for “!=” operator.” what does that mean? don’t boilerplate code repeats existing code?

I accidentally compared a Vector2 with a Vector3, which is invalid. I have edited my original reply to fix this. “Boilerplate code” is referring to code that intends to lay the foundations for future application.

This was the invalid comparison: move != Vector3.ZERO

move in this case is a Vector2 (contains two numbers) while Vector3.ZERO is referring to a Vector3 (contains three numbers). The not equals operator != compares the values of two things, but those things must be the same type of thing.

the player character is now floating and irresponsive to my inputs even after I rewrote the inputs in the input map to correspond with the rewritten script inputs, did the vector 3 in the boilerplate mess with the _physics_prosess function?

It might be worth going back to the basics and understanding how things work in Godot. What made it fall before, and what changed to make it now floating? Tutorials and forum posts often tell you what to do, but understanding why you do it that way is important.

I’d checking out the example project below. It has everything you need to know about setting up basic movement, and then come back to visit that code example and see how you can modify your setup with the camera in mind.