Skip to content

Commit

Permalink
minor changes on first person controller to avoid jitter when moving …
Browse files Browse the repository at this point in the history
…camera on the current setup
  • Loading branch information
ninetailsrabbit committed Dec 26, 2024
1 parent c728287 commit 60d02b2
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func _process(_delta: float) -> void:

if actor.camera:
fov_label.text = str(actor.camera.fov)



func on_state_changed(from: MachineState, to: MachineState) -> void:
state_label.text = "State: %s -> [%s]" % [from.name, to.name]

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(0, 0.75, 0)]
"values": [Vector3(0, 1.75, 0)]
}

[sub_resource type="Animation" id="Animation_p5yay"]
Expand Down Expand Up @@ -142,7 +142,7 @@ streams_count = 1
stream_0/stream = SubResource("AudioStream_sjrlx")

[node name="FirstPersonController" type="CharacterBody3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.879968, 0)
process_mode = 1
collision_layer = 2
collision_mask = 21
script = ExtResource("1_v7v7g")
Expand Down Expand Up @@ -229,22 +229,25 @@ actor = NodePath("../../..")
gravity_force = 0.6

[node name="StandCollisionShape" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = SubResource("CapsuleShape3D_1od8w")

[node name="CrouchCollisionShape" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.291229, 0)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.708771, 0)
shape = SubResource("CapsuleShape3D_n3iq5")

[node name="CrawlCollisionShape" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.398812, 0)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.601188, 0)
shape = SubResource("CapsuleShape3D_ftet6")

[node name="CeilShapeCast" type="ShapeCast3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = SubResource("SphereShape3D_plvkn")
target_position = Vector3(0, 0.65, 0)
debug_shape_custom_color = Color(0, 0.854902, 0, 1)

[node name="WallCheckers" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)

[node name="FrontCloseWallChecker" type="RayCast3D" parent="WallCheckers"]
unique_name_in_owner = true
Expand Down Expand Up @@ -283,7 +286,7 @@ target_position = Vector3(-1.3, 0, 0.8)
debug_shape_custom_color = Color(0.78019, 0.402978, 0.179512, 1)

[node name="CameraController" type="Node3D" parent="." node_paths=PackedStringArray("actor", "camera", "bob_head")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.75, 0)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.75, 0)
script = ExtResource("5_2gxvf")
actor = NodePath("..")
camera = NodePath("Head/CameraShake3D")
Expand Down Expand Up @@ -323,7 +326,7 @@ script = ExtResource("21_q3kyc")
camera = NodePath("../../../../../../..")

[node name="FootstepsManager3D" type="Node3D" parent="." node_paths=PackedStringArray("floor_detector_raycast", "sound_queue")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.20535, 0)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.79465, 0)
script = ExtResource("24_8j70g")
floor_detector_raycast = NodePath("FloorDetectorRaycast")
sound_queue = NodePath("SoundQueue")
Expand All @@ -338,4 +341,5 @@ audio_stream = SubResource("AudioStreamRandomizer_jgm50")

[node name="AudioStreamPlayer3D" type="AudioStreamPlayer3D" parent="FootstepsManager3D/SoundQueue"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.06458, 0)
stream = SubResource("AudioStreamRandomizer_jgm50")
bus = &"SFX"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@icon("res://components/motion/3D/first-person/controller/mechanics/camera_controller_3d.svg")
class_name CameraController3D extends Node3D

@export var actor: FirstPersonController
@export var actor: Node3D
@export var camera: Camera3D
## 0 Means the rotation on the Y-axis is not limited
@export_range(0, 360, 1, "degrees") var camera_vertical_limit = 89
Expand All @@ -10,20 +10,28 @@ class_name CameraController3D extends Node3D
@export_group("Swing head")
@export var swing_head_enabled: bool = true:
set(value):
swing_head_enabled = value
if not swing_head_enabled and not bob_enabled:
set_physics_process(false)
if value != swing_head_enabled:
swing_head_enabled = value
set_physics_process(swing_head_enabled or (bob_enabled and bob_head != null))

@export_range(0, 360.0, 0.01) var swing_rotation_degrees = 1.5
@export var swing_lerp_factor = 5.0
@export var swing_lerp_recovery_factor = 7.5
@export_group("Bob head")
@export var bob_enabled: bool = true:
set(value):
bob_enabled = value
if not swing_head_enabled and not bob_enabled:
set_physics_process(false)
@export var bob_head: Node3D
if value != bob_enabled:
bob_enabled = value
set_physics_process(swing_head_enabled or (bob_enabled and bob_head != null))

@export var bob_head: Node3D:
set(value):
if value != bob_head:
bob_head = value

if bob_head != null:
original_head_bob_position = bob_head.position

@export var bob_speed: float = 10.0
@export var bob_intensity: float = 0.03
@export var bob_lerp_speed = 5.0
Expand Down Expand Up @@ -53,43 +61,50 @@ func _unhandled_input(event: InputEvent) -> void:
var motion: InputEventMouseMotion = event.xformed_by(get_tree().root.get_final_transform())
last_mouse_input = motion.relative

rotate_camera(motion)


func _ready() -> void:
assert(actor is FirstPersonController, "CameraController: This node needs a FirstPersonController node referenced to apply the camera movement")
assert(actor is Node3D, "CameraController: actor Node3D is not set, this camera controller needs a reference to apply the camera movement")

set_physics_process(swing_head_enabled or (bob_enabled and bob_head != null))

current_horizontal_limit = camera_horizontal_limit
current_vertical_limit = camera_vertical_limit
original_camera_rotation = camera.rotation
original_head_bob_position = bob_head.position

mouse_sensitivity = SettingsManager.get_accessibility_section("mouse_sensitivity")
if bob_head != null:
original_head_bob_position = bob_head.position

mouse_sensitivity = SettingsManager.get_accessibility_section(GameSettings.MouseSensivitySetting)

SettingsManager.updated_setting_section.connect(on_mouse_sensitivity_changed)


func _physics_process(delta: float) -> void:
swing_head(delta)
headbob(delta)


func _process(_delta: float) -> void:
rotate_camera(last_mouse_input)


func rotate_camera(motion: InputEventMouseMotion) -> void:
var mouse_sens = mouse_sensitivity / 1000 # radians/pixel, 3 becomes 0.003
func rotate_camera(motion: Vector2) -> void:
var mouse_sens: float = mouse_sensitivity / 1000 # radians/pixel, 3 becomes 0.003

var twistInput = motion.relative.x * mouse_sens ## Giro
var pitchInput = motion.relative.y * mouse_sens ## Cabeceo

var mouse_rotation: Vector3 = Vector3(-pitchInput, -twistInput, 0)
var twist_input: float = motion.x * mouse_sens ## Giro
var pitch_input: float = motion.y * mouse_sens ## Cabeceo

actor.rotate_y(mouse_rotation.y)
rotate_x(mouse_rotation.x)
actor.rotate_y(-twist_input)
rotate_x(-pitch_input)

actor.rotation_degrees.y = limit_horizontal_rotation(actor.rotation_degrees.y)
rotation_degrees.x = limit_vertical_rotation(rotation_degrees.x)

orthonormalize()

#actor.orthonormalize()
#orthonormalize()
#
last_mouse_input = Vector2.ZERO


func limit_vertical_rotation(angle: float) -> float:
if current_vertical_limit > 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class_name Jump extends AirState
@export var jump_times: int = 1
@export var height_reduced_by_jump = 0.2
@export var shorten_jump_on_input_release = true

@export var jump_height: float = 2.5:
get:
return jump_height
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class_name Idle extends GroundState



func physics_update(delta):
super.physics_update(delta)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ var current_weapon: WeaponDatabase.WeaponRecord:
current_state = WeaponHolderStates.Dismantle
else:
current_state = WeaponHolderStates.Neutral


set_physics_process(current_weapon != null)


func _unhandled_input(_event: InputEvent) -> void:
Expand All @@ -79,6 +80,8 @@ func _ready() -> void:
original_holder_position = position
original_camera_fov = camera_controller.camera.fov

set_physics_process(current_weapon != null)


func _physics_process(delta: float) -> void:
if current_weapon:
Expand Down
17 changes: 17 additions & 0 deletions components/motion/3D/first_person_test.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class_name FirstPersonTest extends CharacterBody3D


func _unhandled_key_input(_event: InputEvent) -> void:
if Input.is_action_just_pressed("ui_cancel"):
switch_mouse_capture_mode()


func _ready() -> void:
InputHelper.capture_mouse()


func switch_mouse_capture_mode() -> void:
if InputHelper.is_mouse_visible():
InputHelper.capture_mouse()
else:
InputHelper.show_mouse_cursor()
25 changes: 25 additions & 0 deletions components/motion/3D/first_person_test.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[gd_scene load_steps=4 format=3 uid="uid://cw0qunqva1mls"]

[ext_resource type="Script" path="res://components/motion/3D/first_person_test.gd" id="1_fbvsi"]
[ext_resource type="Script" path="res://components/motion/3D/first-person/controller/mechanics/camera_controller_3d.gd" id="2_jcn33"]

[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_u8j2i"]

[node name="FirstPersonTest" type="CharacterBody3D"]
script = ExtResource("1_fbvsi")

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = SubResource("CapsuleShape3D_u8j2i")

[node name="CameraController3D" type="Node3D" parent="." node_paths=PackedStringArray("actor", "camera")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0)
script = ExtResource("2_jcn33")
actor = NodePath("..")
camera = NodePath("Head/Camera3D")
swing_head_enabled = false
bob_enabled = false

[node name="Head" type="Node3D" parent="CameraController3D"]

[node name="Camera3D" type="Camera3D" parent="CameraController3D/Head"]

0 comments on commit 60d02b2

Please sign in to comment.