I am following the FPS game in godot tutorial by stayathomedev. I Almost finished the first video but once I had typed the code to be able to move the camera with my mouse, it never worked. There are no errors in my code, but I cant move the camera with the code that was provided in the tutorial. Timestamp: video 1 "Make An FPS in Godot 4, 10:00 / 13:25
My following Code
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") 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()
func _input(event):
if event.is_action_pressed("exit"):
get_tree().quit()
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
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_input(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))
@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
@export var MOUSE_SENSITIVITY : float = 0.5
func _update_camera(delta):
_mouse_rotation.x += _tilt_input * delta
_mouse_rotation.x = clamp(_mouse_rotation.x, TILT_UPPER_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
_update_camera(delta)
what do you mean?
could you maybe type the code that you are suggesting there? Iâm new, this is the first game that I am making so I donât really understand.
Thank you, but it wont let me run it because line 71 has " Invalid access to property or key âtransformâ on a base object of type âNilâ."
line 71 is:
Oh, yeah I assigned it, when I click the âtestâ button I can move the camera and see the sun. But I donât see my objects, and yes I did give them collision shapes.