I need help with this

Its giving me errors and I don’t know how to fix. I’m new. also I used A.I with some of it

extends CharacterBody3D

@export var mouse_sensitivity := 0.0025
@export var move_speed := 5.0
@export var jump_strength := 6.5
@export var gravity := 16.0
@export var pitch_limit := 1.5 # ~85 degrees in radians

Double jump flags

var can_jump := false
var can_double_jump := false

Look angles

var yaw := 0.0
var pitch := 0.0

@onready var camera = $Camera3D

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

func _unhandled_input(event):
if event is InputEventMouseMotion:
yaw -= event.relative.x * mouse_sensitivity
pitch -= event.relative.y * mouse_sensitivity
pitch = clamp(pitch, -pitch_limit, pitch_limit)

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

func _physics_process(delta):
# Handle rotation
rotation.y = yaw
camera.rotation.x = pitch

# Movement input
var input_dir = Vector3.ZERO
var forward = -transform.basis.z
var right = transform.basis.x

if Input.is_action_pressed("Move_Forward"):
	input_dir += forward
if Input.is_action_pressed("Move_Backward"):
	input_dir -= forward
if Input.is_action_pressed("Move_Left"):
	input_dir -= right
if Input.is_action_pressed("Move_Right"):
	input_dir += right

input_dir = input_dir.normalized()
var horizontal_velocity = input_dir * move_speed
velocity.x = horizontal_velocity.x
velocity.z = horizontal_velocity.z

# Apply gravity
if not is_on_floor():
	velocity.y -= gravity * delta
else:
	velocity.y = 0
	can_jump = true
	can_double_jump = true

# Jumping
if Input.is_action_just_pressed("jump"):
	if can_jump:
		velocity.y = jump_strength
		can_jump = false
	elif can_double_jump:
		velocity.y = jump_strength
		can_double_jump = false

move_and_slide()

There is a fair amount of code here. Could you explain a little more? For instance what errors is it giving you? What did you expect to happen compared to what happened?

PS
Using AI is fine, but as a language model based on predictive text you rarely get fully working code from AI. What it sometimes is good at is suggesting approaches with its example code that you might not have thought of, or methods that you were unaware of. However it is just as likely to make up a method that does not exist, or suggest solutions that are very tortuous or long-winded or quite often are simply the wrong approach.

If you are new, I would suggest following a few tutorials first, then starting out by applying what you have learned to your own new (and tiny) practice projects. AI is an amazing tool, but it cannot be relied on to act as a tutor or mentor in game development.

2 Likes

It says "Invalid access to property or key ‘rotation’ on a base object of type ‘null instance’

seems like the $Camera3D is missing from your player scene, could you please recheck it in the scene tree, if it exists or has same name? Also make sure if it not deleted/queued by codes somewhere.

If the codes is generated by AI, you would need to replace the $Camera3d with your actual Camera3D node path in your player scene.

it says now the same message just it’s on a base of Camera3D

rotation.y = yaw

Probably should be

camera.rotation.y = yaw
1 Like