Hi everyone,
I’m currently building a 3D character controller in Godot using this script:
extends CharacterBody3D
@export var SPEED = 7.0
@export var SPRINT_SPEED = 10.0
@export var JUMP_VELOCITY = 5.5
@onready var camera = $camera_mount/Camera3D
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity.y -= ProjectSettings.get_setting(“physics/3d/default_gravity”) * delta
if Input.is_action_just_pressed(“jump”) and is_on_floor():
velocity.y = JUMP_VELOCITY
var input_dir = Input.get_vector(“move_left”, “move_right”, “move_forward”, “move_backward”)
var direction = (camera.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
var current_speed = SPEED
if Input.is_action_pressed(“sprint”):
current_speed = SPRINT_SPEED
if direction != Vector3.ZERO:
velocity.x = direction.x * current_speed
velocity.z = direction.z * current_speed
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
Here’s the problem:
• When I run the game and press any movement key (W, A, S, D), the character always moves backward, no matter which key I press.
• Sometimes the movement becomes extremely slow-motion and unresponsive.
• If I slightly modify the script, the character either stops moving completely, or continues moving backward on its own even when no key is pressed.
• In some cases, Godot shows errors like “Invalid access to property or key ‘transform’ on a base object of type ’null instance’.”
I’ve double-checked the node paths (e.g. camera_mount/Camera3D) and the inputs are set correctly in the Input Map.
Any idea what might be causing this?
Thanks in advance!