Error when key for movement/esc pressed

Godot Version

godot 4.4

Question

i made a 3d fps movement, but whenever i click one of the buttons to move or esc to chandge the mouse mode, the game chrashes and turns off

here is my script:

extends CharacterBody3D

@export var playerSpeeed = 5.0
@export var jumpForce = 10.0
@export var gravity = 8.0
@export var mouseSensivity = 0.5

@onready var head = $Head
@onready var camera = $Head/Camera3D

var direction = Vector3.ZERO
var camera_x_angel = 0.0

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)


func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		head.rotate_y(-deg_to_rad(event.relative.x * mouseSensivity))



	camera_x_angel += event.relative.y * mouseSensivity
	camera_x_angel = clamp(camera_x_angel, -90.0, 90.0)
	camera.rotation.x = -deg_to_rad(camera_x_angel)

	if event is InputEventKey:
		if event.scancode == KEY_ESCAPE and event.pressed:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

func _process(delta):
	direction = Input.get_axis("left", "right") * head.basis.x + Input.get_axis("forward", "backward") * head.basis.z
	velocity = direction.normalized() * playerSpeeed + velocity.y * Vector3.UP


func _physics_process(delta: float) -> void:
	var input_direction: Vector2 = Input.get_vector("left", "right", "forward", "backward")
	var direction: Vector3 = head.basis * Vector3(input_direction.x, 0, input_direction.y)

	# you should use a seperate horizontal/vertical velocity so you can preserve gravity
	var vertical_velocity := velocity.project(up_direction)
	var horizontal_velocity := velocity - vertical_velocity

	# assign player movement on a horizontal plane, not overwriting gravity
	horizontal_velocity = direction * playerSpeeed

	# gravity and jumping, using vertical_velocity
	if Input.is_action_just_pressed("jump") and is_on_floor():
		vertical_velocity.y += jumpForce
	else:
		vertical_velocity.y -= gravity * delta

	# re-combine horizontal and vertical velocity
	velocity = vertical_velocity + horizontal_velocity
	move_and_slide()

Putting three backticks (```) on lines before and after your code will make it format better in your post. You can hit “edit” to fix that…

What error are you getting in the debugger?

W 0:00:07:945 The parameter “delta” is never used in the function “_process()”. If this is intended, prefix it with an underscore: “_delta”.
UNUSED_PARAMETER
player.gd:30

and

W 0:00:07:964 The local variable “direction” is shadowing an already-declared variable at line 11 in the current class.
SHADOWED_VARIABLE
player.gd:37

The warning about delta can be ignored, or you can make the function argument _delta to make the warning go away.

The shadowing over direction is because on line 11 you have a var direction that’s global to your class, then inside _physics_process() you declare another var direction (line 37) which means inside that function you can’t access the outer direction.

I don’t think either of those should cause a crash, though.

the delta error is now gone, but the game still crashes. and i don’t know how to fix the direction variable error. because whenever i try to delete one of the “var”, there is another error wich makes me unable to load the game

Fixing the direction thing is a question of understanding whether you want the two versions of direction to be different variables, or whether you want _physics_process() to be changing the global direction variable.

If you want to keep both, then you need to rename the one that’s inside the function:

var direction = Vector3.ZERO

[...]

func _physics_process(_delta: float) -> void:
    var new_dir: Vector3 = head.basis * ...

If you want only one, take var out of the function one:

var direction = Vector3.ZERO

[...]

func _phsysics_process(_delta: float) -> void:
    direction = head.basis * ...

What’s the error that makes you unable to load the game if you do that? And when it crashes, if you look at the debugger output for errors, do you see anything?