Bug in Mouse Movement

Im really new to Godot and its already amazing! But when working on a 3D game, I kept getting this error:

Line 4:Unexpected “Identifier” in class body.

What? I checked with ChatGPT but, its Ai so didn’t really help. I put it on the camera. Please help me. Heres my code:

extends CharacterBody3D

@export var speed: float = 5.0 # Movement speed
@export var mouse_sensitivity: float = 0.2

Camera node for vertical rotation

onready var camera = $Camera3D

Rotation variables for mouse look

var pitch: float = 0.0 # X-axis (vertical rotation)
var yaw: float = 0.0 # Y-axis (horizontal rotation)

func _ready():
Input.set_mouse_mode(Input.MouseMode.CAPTURED) # Lock the mouse cursor to the screen

func _input(event):
if event is InputEventMouseMotion:
# Get mouse delta and apply sensitivity for horizontal and vertical rotation
var mouse_delta = event.relative * mouse_sensitivity

	# Adjust yaw (horizontal rotation)
	yaw -= mouse_delta.x
	rotation.y = deg2rad(yaw)

	# Adjust pitch (vertical rotation), clamped to avoid flipping
	pitch = clamp(pitch - mouse_delta.y, -89, 89)
	camera.rotation.x = deg2rad(pitch)

func _physics_process(delta):
var direction = Vector3()

# Get input for movement
if Input.is_action_pressed("move_forward"):
	direction -= transform.basis.z
if Input.is_action_pressed("move_backward"):
	direction += transform.basis.z
if Input.is_action_pressed("move_left"):
	direction -= transform.basis.x
if Input.is_action_pressed("move_right"):
	direction += transform.basis.x

# Normalize direction and apply speed
direction = direction.normalized()
velocity.x = direction.x * speed
velocity.z = direction.z * speed

# Move the character
move_and_slide(velocity)

onready needs an @ in Godot 4.x, sadly ChatGPT often recommends mixing 3.x and 4.x code

Make sure to paste code with proper formatting

3 Likes