FPS Tutorial Error

Godot Version

4.2

Question

I was following a tutorial from StayAtHomeDev and got the error: Invalid get index ‘transform’ (on base: ‘Nil’). Here is the code:

extends CharacterBody3D

const SPEED = 60.0
const JUMP_VELOCITY = 30.0

@export var TILT_LOWER_LIMIT := deg_to_rad(-90.0)
@export var TILT_UPPER_LIMIT := deg_to_rad(90.0)
@export var CAMERA_CONTROLLER : Camera3D

var _mouse_input : bool = false
var _mouse_rotation : Vector3
var _rotation_input : float
var _tilt_input : float

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)

func _input(event):
if event.is_action_pressed(“exit”):
get_tree().quit()

func _unhandled_input(event):
_mouse_input = event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
if _mouse_input:
_rotation_input = -event.relative.x
_tilt_input = event.relative.y
print(Vector2(_rotation_input,_tilt_input))

func _update_camera(delta):

#Rotate camera using euler rotation
_mouse_rotation.x += _tilt_input * delta
_mouse_rotation.x = clamp(_mouse_rotation.x, TILT_LOWER_LIMIT, TILT_UPPER_LIMIT)
_mouse_rotation.y += _rotation_input * delta

CAMERA_CONTROLLER.transform.basis = Basis.from_euler(_mouse_rotation)
CAMERA_CONTROLLER.rotation.z = 0.0

_rotation_input = 0.0
_tilt_input = 0.0

func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta

_update_camera(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("move_left", "move_right", "move_forward", "move_backward")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
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()

What is going on here, can someone please help me?

Did you set the @export variable CAMERA_CONTROLLER? Select your character and check the inspector, click “assign” on the Camera Controller property you’ve created.

Thank you, this is my first time creating a 3d game, and I was afraid that I had bitten off more than I could chew. :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.