Godot Version
4.6.2 On Steam
Question
I was following this video to make my camera work like a FPS game, but the player's movement won't follow the mouse. My best guess is that it don't work because I wasn't following the video from the beginning, but I am very new to this so I don't know what to do. I started the video about 2:19 and finished around 12:00 skipping the non-camera/mouse related content. Video- https://www.youtube.com/watch?v=N-jh8qc8tJs&t=582s
extends CharacterBody3D
@export var speed = 15
@export var fall_acceleration = 75
var target_velocity = Vector3.ZERO
var mouse_input: bool = false
var rotation_input: float
var tilt_input: float
var mouse_rotation : Vector3
var player_rotation : Vector3
var camera_rotation : Vector3
@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)
@export var CAMERA_CONTROLLER : Camera3D
func _input(event):
if event.is_action_pressed("exit"):
get_tree().quit()
func _ready ():
Input.mouse_mode =Input.MOUSE_MODE_CAPTURED
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
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 _physics_process(delta):
var direction = Vector3.ZERO
_update_camera(delta)
if Input.is_action_pressed("move_right"):
direction.x += 1
if Input.is_action_pressed("move_left"):
direction.x -= 1
if Input.is_action_pressed("move_back"):
direction.z += 1
if Input.is_action_pressed("move_forward"):
direction.z -= 1
if direction != Vector3.ZERO:
direction= direction.normalized()
target_velocity.x = direction.x * speed
target_velocity.z = direction.z * speed
if not is_on_floor(): #gravity
target_velocity.y= target_velocity.y -(fall_acceleration * delta)
velocity = target_velocity
move_and_slide()