First time making 3rd person camera need help

Godot Version

Godot 4.6.2

Question

I’m trying to make movement inputs relative to camera, but I’m not sure what “Identifier “camera” not declared in the current scope.” mean do I need to rename something or get a definition from the camera script/ Node tree?

extends CharacterBody3D


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("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()

I don’t have much idea about the movement code itself because I haven’t really dabbled with 3D stuff in Godot yet, but I can confidently tell you that your main problem is the fact that this script does not know what camera is.

The easiest way to get a reference to the camera is to drag the camera node from the scene tree into the script itself, making sure to hold ctrl (cmd on Mac) before dropping the camera node in the script, but not when going to drag the node. It will automatically create a reference to the camera for you to use in your script.

Depending on how your scene tree is organized, it might look a little something like this:

@onready var camera = $"Camera"

It may or may not look exactly like this, but this should work.

The reason you need to do this is actually hinted at in the error you’re getting, and it might be helpful to look into what “scope” means in case you ever run into similar issues.

To put it simply: “Scope” is like a stingy box that can receive items from the outside, but will not give items to the outside.

For example, consider the following code

var outside_variable: int = 5

func _ready():
     print(outside_variable)
     some_function()
     print(inside_variable)

func some_function():
     var inside_variable: String = "This is MY string only!"
     print(outside_variable)
     print(inside_variable)

When the _ready function is called, the script will run through each function and act accordingly. It will successfully run the first print function, outputting 5 to the terminal, then it will run some_function, which declares and sets inside_variable, prints 5 to the terminal, and then prints This is MY string only! to the terminal. Everything has worked fine so far, but problems arise when it tries to run that last print function in the _ready function.

The _ready function simply does not have access to inside_variable, and thus will throw an error. some_function is being greedy and refuses to let anyone outside of the Scope of the function access the variable.

If inside_variable was instead declared with outside_variable, everything would work just fine, and some_function wouldn’t be able to gate-keep the variable.

To shut down some confusion, the reason why your script can access stuff like velocity or is_on_floor() just fine, even though you haven’t declared any of them in your script, is that they comes with the CharacterBody3D class, which gets them from some other parent class.

Scope can be somewhat tricky to wrap your head around (I’d be lying if I said it hasn’t tripped me up a few times in the past), but as you refine your skills and do some more programming, considering the Scope of a variable or function will start to become second nature.

Would you like I recommend a YouTube tutorial?

The current camera 3d (if any) is available as get_viewport().get_camera_3d()

yea thanks

it didn’t really work; my camera node is on a 3d node called SpringArmPivot does that messes with it?

why doesn’t it work? Can you post your updated code, any error messages? Maybe the scene tree too?

literally nothing changes from when I hashtag it out

the cam part of my player tree:

SpringArmPivot(Node3d) (where the camera movement script is)

its children are a SpringArm3d with its own child SpringPosition(Node 3d) and the camera3d Node(camera) (where the camera follow script is)

neither of these scripts have an issue as far as I know.