9 Commits
2.3 ... 2.4.1

4 changed files with 229 additions and 50 deletions

View File

@ -14,7 +14,7 @@ Move with WASD, space to jump, shift to sprint, C to crouch.
- In-air momentum
- Motion smoothing
- FOV smoothing
- Head bobbing
- Movement animations
- Crouching
- Sprinting
- 2 crosshairs/reticles, one is animated (more to come?)
@ -44,10 +44,9 @@ Click on the character node and there should be settings in the "Feature Setting
- Use the `is_on_floor` function to tell if the player is standing or falling.
**How to change reticles (crosshairs):**
- Find the folder at `res://addons/fpc/reticles`.
- Pick one and add it to the `UserInterface` node in the character scene. You may need to right click on the character and enable "Editable Children".
- Set the Character value to your character node. (it's under the Nodes group)
- Change the `anchors_preset` value on the reticle to Center.
Change the "Default Reticle" setting to your reticle file.
During runtime:
Use the `change_reticle` function on the character.
**How to create a new reticle:**
- Choose a reticle to base it off of.
@ -55,4 +54,3 @@ Click on the character node and there should be settings in the "Feature Setting
- Remove the script from the reticle and create a new one. (for some reason you have to do this)
- Edit the reticle to your needs.
- Follow the "how to change reticles" directions to use it.

View File

@ -10,8 +10,8 @@ extends CharacterBody3D
@export var acceleration : float = 10.0
@export var jump_velocity : float = 4.5
@export var mouse_sensitivity : float = 0.1
@export var initial_facing_direction : Vector3 = Vector3.ZERO
@export var immobile : bool = false
@export_file var default_reticle
@export_group("Nodes")
@export var HEAD : Node3D
@ -39,7 +39,6 @@ extends CharacterBody3D
#@export var LOOK_DOWN : String
@export_group("Feature Settings")
@export var immobile : bool = false
@export var jumping_enabled : bool = true
@export var in_air_momentum : bool = true
@export var motion_smoothing : bool = true
@ -51,6 +50,7 @@ extends CharacterBody3D
@export var continuous_jumping : bool = true
@export var view_bobbing : bool = true
@export var jump_animation : bool = true
@export var pausing_enabled : bool = true
# Member variables
var speed : float = base_speed
@ -60,6 +60,8 @@ var state : String = "normal"
var low_ceiling : bool = false # This is for when the cieling is too low and the player needs to crouch.
var was_on_floor : bool = true
var RETICLE : Control
# Get the gravity from the project settings to be synced with RigidBody nodes
var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity") # Don't set this as a const, see the gravity section in _physics_process
@ -67,13 +69,53 @@ var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
# Set the camera rotation to whatever initial_facing_direction is
if initial_facing_direction:
HEAD.set_rotation_degrees(initial_facing_direction) # I don't want to be calling this function if the vector is zero
HEAD.rotation = rotation
rotation = Vector3.ZERO
if default_reticle:
change_reticle(default_reticle)
# Reset the camera position
HEADBOB_ANIMATION.play("RESET")
JUMP_ANIMATION.play("RESET")
CROUCH_ANIMATION.play("RESET")
check_controls()
func check_controls(): # If you add a control, you might want to add a check for it here.
if !InputMap.has_action(JUMP):
push_error("No control mapped for jumping. Please add an input map control. Disabling jump.")
jumping_enabled = false
if !InputMap.has_action(LEFT):
push_error("No control mapped for move left. Please add an input map control. Disabling movement.")
immobile = true
if !InputMap.has_action(RIGHT):
push_error("No control mapped for move right. Please add an input map control. Disabling movement.")
immobile = true
if !InputMap.has_action(FORWARD):
push_error("No control mapped for move forward. Please add an input map control. Disabling movement.")
immobile = true
if !InputMap.has_action(BACKWARD):
push_error("No control mapped for move backward. Please add an input map control. Disabling movement.")
immobile = true
if !InputMap.has_action(PAUSE):
push_error("No control mapped for move pause. Please add an input map control. Disabling pausing.")
pausing_enabled = false
if !InputMap.has_action(CROUCH):
push_error("No control mapped for crouch. Please add an input map control. Disabling crouching.")
crouch_enabled = false
if !InputMap.has_action(SPRINT):
push_error("No control mapped for sprint. Please add an input map control. Disabling sprinting.")
sprint_enabled = false
func change_reticle(reticle):
if RETICLE:
RETICLE.queue_free()
RETICLE = load(reticle).instantiate()
RETICLE.character = self
$UserInterface.add_child(RETICLE)
func _physics_process(delta):
@ -112,7 +154,11 @@ func _physics_process(delta):
if jump_animation:
if !was_on_floor and is_on_floor(): # Just landed
JUMP_ANIMATION.play("land")
match randi() % 2:
0:
JUMP_ANIMATION.play("land_left", 0.25)
1:
JUMP_ANIMATION.play("land_right", 0.25)
was_on_floor = is_on_floor() # This must always be at the end of physics_process
@ -122,12 +168,12 @@ func handle_jumping():
if continuous_jumping:
if Input.is_action_pressed(JUMP) and is_on_floor() and !low_ceiling:
if jump_animation:
JUMP_ANIMATION.play("jump")
JUMP_ANIMATION.play("jump", 0.25)
velocity.y += jump_velocity
else:
if Input.is_action_just_pressed(JUMP) and is_on_floor() and !low_ceiling:
if jump_animation:
JUMP_ANIMATION.play("jump")
JUMP_ANIMATION.play("jump", 0.25)
velocity.y += jump_velocity
@ -231,16 +277,25 @@ func update_camera_fov():
func headbob_animation(moving):
if moving and is_on_floor():
var use_headbob_animation : String
match state:
"normal","crouching":
use_headbob_animation = "walk"
"sprinting":
use_headbob_animation = "sprint"
var was_playing : bool = false
if HEADBOB_ANIMATION.current_animation == "headbob":
if HEADBOB_ANIMATION.current_animation == use_headbob_animation:
was_playing = true
HEADBOB_ANIMATION.play("headbob", 0.25)
HEADBOB_ANIMATION.play(use_headbob_animation, 0.25)
HEADBOB_ANIMATION.speed_scale = (current_speed / base_speed) * 1.75
if !was_playing:
HEADBOB_ANIMATION.seek(float(randi() % 2)) # Randomize the initial headbob direction
# Let me explain that piece of code because it looks like it does the opposite of what it actually does.
# The headbob animation has two starting positions. One is at 0 and the other is at 1.
# randi() % 2 returns either 0 or 1, and so the animation randomly starts at one of the starting positions.
# This code is extremely performant but it makes no sense.
else:
HEADBOB_ANIMATION.play("RESET", 0.25)
@ -254,11 +309,12 @@ func _process(delta):
status += " in the air"
$UserInterface/DebugPanel.add_property("State", status, 4)
if Input.is_action_just_pressed(PAUSE):
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
elif Input.mouse_mode == Input.MOUSE_MODE_VISIBLE:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
if pausing_enabled:
if Input.is_action_just_pressed(PAUSE):
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
elif Input.mouse_mode == Input.MOUSE_MODE_VISIBLE:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
HEAD.rotation.x = clamp(HEAD.rotation.x, deg_to_rad(-90), deg_to_rad(90))

View File

@ -1,7 +1,6 @@
[gd_scene load_steps=19 format=3 uid="uid://cc1m2a1obsyn4"]
[gd_scene load_steps=20 format=3 uid="uid://cc1m2a1obsyn4"]
[ext_resource type="Script" path="res://addons/fpc/character.gd" id="1_0t4e8"]
[ext_resource type="PackedScene" uid="uid://3mij3cjhkwsm" path="res://addons/fpc/reticles/reticle_1.tscn" id="2_uuexm"]
[ext_resource type="Script" path="res://addons/fpc/debug.gd" id="3_x1wcc"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_kp17n"]
@ -14,30 +13,67 @@ material = SubResource("StandardMaterial3D_kp17n")
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_uy03j"]
[sub_resource type="Animation" id="Animation_pqev3"]
[sub_resource type="Animation" id="Animation_j8cx7"]
resource_name = "RESET"
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Mesh:position")
tracks/0/interp = 1
tracks/0/path = NodePath("Mesh:scale")
tracks/0/interp = 2
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(0, 1, 0)]
"values": [Vector3(1, 1, 1)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Head:position")
tracks/1/interp = 1
tracks/1/path = NodePath("Collision:scale")
tracks/1/interp = 2
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(1, 1, 1)]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Mesh:position")
tracks/2/interp = 2
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(0, 1, 0)]
}
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("Collision:position")
tracks/3/interp = 2
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(0, 1, 0)]
}
tracks/4/type = "value"
tracks/4/imported = false
tracks/4/enabled = true
tracks/4/path = NodePath("Head:position")
tracks/4/interp = 2
tracks/4/loop_wrap = true
tracks/4/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(0, 1.5, 0)]
}
@ -107,7 +143,7 @@ tracks/4/keys = {
[sub_resource type="AnimationLibrary" id="AnimationLibrary_5e5t5"]
_data = {
"RESET": SubResource("Animation_pqev3"),
"RESET": SubResource("Animation_j8cx7"),
"crouch": SubResource("Animation_5ec5e")
}
@ -149,8 +185,8 @@ tracks/2/keys = {
"times": PackedFloat32Array(0)
}
[sub_resource type="Animation" id="Animation_lrqmv"]
resource_name = "headbob"
[sub_resource type="Animation" id="Animation_8ku67"]
resource_name = "sprint"
length = 2.0
loop_mode = 1
tracks/0/type = "bezier"
@ -160,8 +196,46 @@ tracks/0/path = NodePath("Camera:position:x")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"handle_modes": PackedInt32Array(0, 1, 0, 1, 0),
"points": PackedFloat32Array(0.06, -0.25, 0, 0.25, -0.01, 0, 0, 0, 0, 0, -0.06, -0.25, 0.01, 0.25, 0.01, 0, 0, 0, 0, 0, 0.06, -0.25, -0.01, 0.25, 0),
"times": PackedFloat32Array(0, 0.5, 1, 1.5, 2)
}
tracks/1/type = "bezier"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Camera:position:y")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"handle_modes": PackedInt32Array(0, 0, 0, 0, 0),
"points": PackedFloat32Array(0.04, -0.25, 0, 0.25, 0, 0, -0.25, 0, 0.25, 0, -0.04, -0.25, 0, 0.25, 0, 0, -0.25, 0, 0.25, 0, 0.04, -0.25, 0, 0.25, 0),
"points": PackedFloat32Array(0.05, -0.25, 0, 0.2, -0.01, 0, -0.2, 0.000186046, 0.2, 0.000186046, 0.05, -0.2, -0.01, 0.2, -0.01, 0, -0.2, 0, 0.2, 0, 0.05, -0.2, -0.01, 0.25, 0),
"times": PackedFloat32Array(0, 0.5, 1, 1.5, 2)
}
tracks/2/type = "bezier"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Camera:position:z")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"handle_modes": PackedInt32Array(0, 0, 0, 0, 0),
"points": PackedFloat32Array(0, -0.25, 0, 0.25, 0, 0, -0.25, 0, 0.25, 0, 0, -0.25, 0, 0.25, 0, 0, -0.25, 0, 0.25, 0, 0, -0.25, 0, 0.25, 0),
"times": PackedFloat32Array(0, 0.5, 1, 1.5, 2)
}
[sub_resource type="Animation" id="Animation_lrqmv"]
resource_name = "walk"
length = 2.0
loop_mode = 1
tracks/0/type = "bezier"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Camera:position:x")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"handle_modes": PackedInt32Array(0, 1, 0, 1, 0),
"points": PackedFloat32Array(0.04, -0.25, 0, 0.25, 0, 0, 0, 0, 0, 0, -0.04, -0.25, 0, 0.25, 0, 0, 0, 0, 0, 0, 0.04, -0.25, 0, 0.25, 0),
"times": PackedFloat32Array(0, 0.5, 1, 1.5, 2)
}
tracks/1/type = "bezier"
@ -190,7 +264,8 @@ tracks/2/keys = {
[sub_resource type="AnimationLibrary" id="AnimationLibrary_o0unb"]
_data = {
"RESET": SubResource("Animation_gh776"),
"headbob": SubResource("Animation_lrqmv")
"sprint": SubResource("Animation_8ku67"),
"walk": SubResource("Animation_lrqmv")
}
[sub_resource type="Animation" id="Animation_fvvjq"]
@ -207,9 +282,22 @@ tracks/0/keys = {
"update": 0,
"values": [Vector3(0.0349066, 0, 0)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Camera:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(0, 0, 0)]
}
[sub_resource type="Animation" id="Animation_s07ye"]
resource_name = "jump"
length = 3.0
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
@ -217,14 +305,15 @@ tracks/0/path = NodePath("Camera:rotation")
tracks/0/interp = 2
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.5, 1),
"times": PackedFloat32Array(0, 0.6, 3),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector3(0, 0, 0), Vector3(0.0349066, 0, 0), Vector3(0, 0, 0)]
}
[sub_resource type="Animation" id="Animation_vsknp"]
resource_name = "land"
[sub_resource type="Animation" id="Animation_l1rph"]
resource_name = "land_left"
length = 1.5
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
@ -232,17 +321,58 @@ tracks/0/path = NodePath("Camera:rotation")
tracks/0/interp = 2
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.5, 1),
"times": PackedFloat32Array(0, 0.5, 1.5),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector3(0, 0, 0), Vector3(-0.0349066, 0, 0), Vector3(0, 0, 0)]
"values": [Vector3(0, 0, 0), Vector3(-0.0349066, 0, 0.0174533), Vector3(0, 0, 0)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Camera:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0, 0.5, 1.5),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector3(0, 0, 0), Vector3(0, -0.1, 0), Vector3(0, 0, 0)]
}
[sub_resource type="Animation" id="Animation_vsknp"]
resource_name = "land_right"
length = 1.5
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Camera:rotation")
tracks/0/interp = 2
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.5, 1.5),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector3(0, 0, 0), Vector3(-0.0349066, 0, -0.0174533), Vector3(0, 0, 0)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Camera:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0, 0.5, 1.5),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector3(0, 0, 0), Vector3(0, -0.1, 0), Vector3(0, 0, 0)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_qeg5r"]
_data = {
"RESET": SubResource("Animation_fvvjq"),
"jump": SubResource("Animation_s07ye"),
"land": SubResource("Animation_vsknp")
"land_left": SubResource("Animation_l1rph"),
"land_right": SubResource("Animation_vsknp")
}
[sub_resource type="Theme" id="Theme_wdf0f"]
@ -255,6 +385,7 @@ MarginContainer/constants/margin_top = 10
[node name="Character" type="CharacterBody3D" node_paths=PackedStringArray("HEAD", "CAMERA", "HEADBOB_ANIMATION", "JUMP_ANIMATION", "CROUCH_ANIMATION", "COLLISION_MESH")]
script = ExtResource("1_0t4e8")
default_reticle = "res://addons/fpc/reticles/reticle_1.tscn"
HEAD = NodePath("Head")
CAMERA = NodePath("Head/Camera")
HEADBOB_ANIMATION = NodePath("Head/HeadbobAnimation")
@ -287,7 +418,7 @@ transform = Transform3D(1, 0, 0, 0, 0.999391, -0.0348995, 0, 0.0348995, 0.999391
libraries = {
"": SubResource("AnimationLibrary_o0unb")
}
blend_times = [&"RESET", &"RESET", 0.5, &"RESET", &"headbob", 0.5, &"headbob", &"RESET", 0.5]
blend_times = [&"RESET", &"RESET", 0.5, &"RESET", &"walk", 0.5, &"walk", &"RESET", 0.5]
[node name="JumpAnimation" type="AnimationPlayer" parent="Head"]
libraries = {
@ -304,11 +435,6 @@ grow_horizontal = 2
grow_vertical = 2
mouse_filter = 1
[node name="Reticle_1" parent="UserInterface" node_paths=PackedStringArray("reticle_lines", "character") instance=ExtResource("2_uuexm")]
layout_mode = 1
reticle_lines = [NodePath("top"), NodePath("left"), NodePath("right"), NodePath("bottom")]
character = NodePath("../..")
[node name="DebugPanel" type="PanelContainer" parent="UserInterface"]
visible = false
layout_mode = 0
@ -329,5 +455,3 @@ layout_mode = 2
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = SubResource("SphereShape3D_k4wwl")
target_position = Vector3(0, 0.5, 0)
[editable path="UserInterface/Reticle_1"]

View File

@ -73,7 +73,7 @@ func update_reticle_settings():
reticle_lines[3].points[1].y = line_length + line_distance
"
[node name="Reticle" type="CenterContainer"]
[node name="Reticle" type="CenterContainer" node_paths=PackedStringArray("reticle_lines")]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
@ -82,6 +82,7 @@ anchor_bottom = 0.5
grow_horizontal = 2
grow_vertical = 2
script = SubResource("GDScript_a8kpl")
reticle_lines = [NodePath("top"), NodePath("left"), NodePath("right"), NodePath("bottom")]
[node name="dot" type="Polygon2D" parent="."]
polygon = PackedVector2Array(-1, -1, 1, -1, 1, 1, -1, 1)