I am trying to make a fps controller and I tried adding camera rotation but I am getting this error “Invalid access to property or key ‘transform’ on a base object of type ‘Nil’.”
This is the Fps Controller:
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@export var Mouse_Sensitivity : float = 0.5
@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
var _player_rotation : Vector3
var _camera_rotation : Vector3
func _unhandled_inpiut(event):
_mouse_input = event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
if _mouse_input:
_rotation_input = -event.relative.x * Mouse_Sensitivity
_tilt_input = -event.relative.y * Mouse_Sensitivity
print(Vector2(_rotation_input,_tilt_input))
func _update_camera(delta):
_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
_player_rotation = Vector3(0.0,_mouse_rotation.y,0.0)
_camera_rotation = Vector3(_mouse_rotation.x,0.0,0.0)
Camera_Controller.transform.basis = Basis.from_euler(_camera_rotation)
Camera_Controller.rotation.z = 0.0
global_transform.basis = Basis.from_euler(_player_rotation)
_rotation_input = 0.0
_tilt_input = 0.0
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _physics_process(delta: float) → void:
# Add the gravity.
var _input := Vector3.ZERO
if not is_on_floor():
velocity += get_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_backwards")
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()
Anyone know what I did wrong?