Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
2879337ad2 | |||
80476b2757 | |||
53935057cd | |||
02018a0105 | |||
c44be3fbfb | |||
b928d6c76b | |||
468ff36f4d | |||
12cf4d63ab |
@ -11,6 +11,7 @@ Some parts came from StayAtHomeDev's FPS tutorial. You can find that [here](http
|
|||||||
Move with WASD, space to jump, shift to sprint, C to crouch.
|
Move with WASD, space to jump, shift to sprint, C to crouch.
|
||||||
|
|
||||||
**FEATURES:**
|
**FEATURES:**
|
||||||
|
- Extremely configurable
|
||||||
- In-air momentum
|
- In-air momentum
|
||||||
- Motion smoothing
|
- Motion smoothing
|
||||||
- FOV smoothing
|
- FOV smoothing
|
||||||
@ -19,6 +20,7 @@ Move with WASD, space to jump, shift to sprint, C to crouch.
|
|||||||
- Sprinting
|
- Sprinting
|
||||||
- 2 crosshairs/reticles, one is animated (more to come?)
|
- 2 crosshairs/reticles, one is animated (more to come?)
|
||||||
- Controller/GamePad support (enabled through code, see wiki)
|
- Controller/GamePad support (enabled through code, see wiki)
|
||||||
|
- In-editor tools (enable editable children to use)
|
||||||
|
|
||||||
If you make a cool game with this addon, I would love to hear about it!
|
If you make a cool game with this addon, I would love to hear about it!
|
||||||
|
|
||||||
@ -54,3 +56,8 @@ Use the `change_reticle` function on the character.
|
|||||||
- Remove the script from the reticle and create a new one. (for some reason you have to do this)
|
- 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.
|
- Edit the reticle to your needs.
|
||||||
- Follow the "how to change reticles" directions to use it.
|
- Follow the "how to change reticles" directions to use it.
|
||||||
|
|
||||||
|
**How to use the editor tools:**
|
||||||
|
- Enable editable children on the `CharacterBody` node
|
||||||
|
- Use the options in the Properties tab to change things
|
||||||
|
- These changes apply in runtime as well
|
||||||
|
48
addons/fpc/EditorModule.gd
Normal file
48
addons/fpc/EditorModule.gd
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
@tool
|
||||||
|
extends Node
|
||||||
|
|
||||||
|
# This module affects runtime nad
|
||||||
|
|
||||||
|
|
||||||
|
#TODO: Add descriptions
|
||||||
|
@export_category("Controller Editor Module")
|
||||||
|
@export var head_y_rotation : float = 0:
|
||||||
|
set(new_rotation):
|
||||||
|
head_y_rotation = new_rotation
|
||||||
|
HEAD.rotation.y = head_y_rotation
|
||||||
|
update_configuration_warnings()
|
||||||
|
|
||||||
|
@export_group("Nodes")
|
||||||
|
@export var CHARACTER : CharacterBody3D
|
||||||
|
@export var head_path : String = "Head" # From this nodes parent node
|
||||||
|
#@export var CAMERA : Camera3D
|
||||||
|
#@export var HEADBOB_ANIMATION : AnimationPlayer
|
||||||
|
#@export var JUMP_ANIMATION : AnimationPlayer
|
||||||
|
#@export var CROUCH_ANIMATION : AnimationPlayer
|
||||||
|
#@export var COLLISION_MESH : CollisionShape3D
|
||||||
|
|
||||||
|
var HEAD
|
||||||
|
|
||||||
|
|
||||||
|
func _ready():
|
||||||
|
HEAD = get_node("../" + head_path)
|
||||||
|
if Engine.is_editor_hint():
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
HEAD.rotation.y = head_y_rotation
|
||||||
|
|
||||||
|
func _process(delta):
|
||||||
|
if Engine.is_editor_hint():
|
||||||
|
pass
|
||||||
|
|
||||||
|
func _get_configuration_warnings():
|
||||||
|
var warnings = []
|
||||||
|
|
||||||
|
if head_y_rotation > 360:
|
||||||
|
warnings.append("The head rotation is greater than 360")
|
||||||
|
|
||||||
|
if head_y_rotation < 0:
|
||||||
|
warnings.append("The head rotation is less than 0")
|
||||||
|
|
||||||
|
# Returning an empty array gives no warnings
|
||||||
|
return warnings
|
@ -1,7 +1,14 @@
|
|||||||
|
|
||||||
|
# COPYRIGHT Colormatic Studios
|
||||||
|
# MIT licence
|
||||||
|
# Quality Godot First Person Controller v2
|
||||||
|
|
||||||
|
|
||||||
extends CharacterBody3D
|
extends CharacterBody3D
|
||||||
|
|
||||||
# TODO: Add descriptions for each value
|
# TODO: Add descriptions for each value
|
||||||
|
|
||||||
|
|
||||||
@export_category("Character")
|
@export_category("Character")
|
||||||
@export var base_speed : float = 3.0
|
@export var base_speed : float = 3.0
|
||||||
@export var sprint_speed : float = 6.0
|
@export var sprint_speed : float = 6.0
|
||||||
@ -51,6 +58,8 @@ extends CharacterBody3D
|
|||||||
@export var view_bobbing : bool = true
|
@export var view_bobbing : bool = true
|
||||||
@export var jump_animation : bool = true
|
@export var jump_animation : bool = true
|
||||||
@export var pausing_enabled : bool = true
|
@export var pausing_enabled : bool = true
|
||||||
|
@export var gravity_enabled : bool = true
|
||||||
|
|
||||||
|
|
||||||
# Member variables
|
# Member variables
|
||||||
var speed : float = base_speed
|
var speed : float = base_speed
|
||||||
@ -58,8 +67,9 @@ var current_speed : float = 0.0
|
|||||||
# States: normal, crouching, sprinting
|
# States: normal, crouching, sprinting
|
||||||
var state : String = "normal"
|
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 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 was_on_floor : bool = true # Was the player on the floor last frame (for landing animation)
|
||||||
|
|
||||||
|
# The reticle should always have a Control node as the root
|
||||||
var RETICLE : Control
|
var RETICLE : Control
|
||||||
|
|
||||||
# Get the gravity from the project settings to be synced with RigidBody nodes
|
# Get the gravity from the project settings to be synced with RigidBody nodes
|
||||||
@ -67,15 +77,18 @@ var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|||||||
|
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
|
#It is safe to comment this line if your game doesn't start with the mouse captured
|
||||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||||
|
|
||||||
HEAD.rotation = rotation
|
# If the controller is rotated in a certain direction for game design purposes, redirect this rotation into the head.
|
||||||
rotation = Vector3.ZERO
|
HEAD.rotation.y = rotation.y
|
||||||
|
rotation.y = 0
|
||||||
|
|
||||||
if default_reticle:
|
if default_reticle:
|
||||||
change_reticle(default_reticle)
|
change_reticle(default_reticle)
|
||||||
|
|
||||||
# Reset the camera position
|
# Reset the camera position
|
||||||
|
# If you want to change the default head height, change these animations.
|
||||||
HEADBOB_ANIMATION.play("RESET")
|
HEADBOB_ANIMATION.play("RESET")
|
||||||
JUMP_ANIMATION.play("RESET")
|
JUMP_ANIMATION.play("RESET")
|
||||||
CROUCH_ANIMATION.play("RESET")
|
CROUCH_ANIMATION.play("RESET")
|
||||||
@ -109,7 +122,7 @@ func check_controls(): # If you add a control, you might want to add a check for
|
|||||||
sprint_enabled = false
|
sprint_enabled = false
|
||||||
|
|
||||||
|
|
||||||
func change_reticle(reticle):
|
func change_reticle(reticle): # Yup, this function is kinda strange
|
||||||
if RETICLE:
|
if RETICLE:
|
||||||
RETICLE.queue_free()
|
RETICLE.queue_free()
|
||||||
|
|
||||||
@ -119,6 +132,7 @@ func change_reticle(reticle):
|
|||||||
|
|
||||||
|
|
||||||
func _physics_process(delta):
|
func _physics_process(delta):
|
||||||
|
# Big thanks to github.com/LorenzoAncora for the concept of the improved debug values
|
||||||
current_speed = Vector3.ZERO.distance_to(get_real_velocity())
|
current_speed = Vector3.ZERO.distance_to(get_real_velocity())
|
||||||
$UserInterface/DebugPanel.add_property("Speed", snappedf(current_speed, 0.001), 1)
|
$UserInterface/DebugPanel.add_property("Speed", snappedf(current_speed, 0.001), 1)
|
||||||
$UserInterface/DebugPanel.add_property("Target speed", speed, 2)
|
$UserInterface/DebugPanel.add_property("Target speed", speed, 2)
|
||||||
@ -133,28 +147,29 @@ func _physics_process(delta):
|
|||||||
|
|
||||||
# Gravity
|
# Gravity
|
||||||
#gravity = ProjectSettings.get_setting("physics/3d/default_gravity") # If the gravity changes during your game, uncomment this code
|
#gravity = ProjectSettings.get_setting("physics/3d/default_gravity") # If the gravity changes during your game, uncomment this code
|
||||||
if not is_on_floor():
|
if not is_on_floor() and gravity and gravity_enabled:
|
||||||
velocity.y -= gravity * delta
|
velocity.y -= gravity * delta
|
||||||
|
|
||||||
handle_jumping()
|
handle_jumping()
|
||||||
|
|
||||||
var input_dir = Vector2.ZERO
|
var input_dir = Vector2.ZERO
|
||||||
if !immobile:
|
if !immobile: # Immobility works by interrupting user input, so other forces can still be applied to the player
|
||||||
input_dir = Input.get_vector(LEFT, RIGHT, FORWARD, BACKWARD)
|
input_dir = Input.get_vector(LEFT, RIGHT, FORWARD, BACKWARD)
|
||||||
handle_movement(delta, input_dir)
|
handle_movement(delta, input_dir)
|
||||||
|
|
||||||
|
# The player is not able to stand up if the ceiling is too low
|
||||||
low_ceiling = $CrouchCeilingDetection.is_colliding()
|
low_ceiling = $CrouchCeilingDetection.is_colliding()
|
||||||
|
|
||||||
handle_state(input_dir)
|
handle_state(input_dir)
|
||||||
if dynamic_fov:
|
if dynamic_fov: # This may be changed to an AnimationPlayer
|
||||||
update_camera_fov()
|
update_camera_fov()
|
||||||
|
|
||||||
if view_bobbing:
|
if view_bobbing:
|
||||||
headbob_animation(input_dir)
|
headbob_animation(input_dir)
|
||||||
|
|
||||||
if jump_animation:
|
if jump_animation:
|
||||||
if !was_on_floor and is_on_floor(): # Just landed
|
if !was_on_floor and is_on_floor(): # The player just landed
|
||||||
match randi() % 2:
|
match randi() % 2: #TODO: Change this to detecting velocity direction
|
||||||
0:
|
0:
|
||||||
JUMP_ANIMATION.play("land_left", 0.25)
|
JUMP_ANIMATION.play("land_left", 0.25)
|
||||||
1:
|
1:
|
||||||
@ -165,11 +180,11 @@ func _physics_process(delta):
|
|||||||
|
|
||||||
func handle_jumping():
|
func handle_jumping():
|
||||||
if jumping_enabled:
|
if jumping_enabled:
|
||||||
if continuous_jumping:
|
if continuous_jumping: # Hold down the jump button
|
||||||
if Input.is_action_pressed(JUMP) and is_on_floor() and !low_ceiling:
|
if Input.is_action_pressed(JUMP) and is_on_floor() and !low_ceiling:
|
||||||
if jump_animation:
|
if jump_animation:
|
||||||
JUMP_ANIMATION.play("jump", 0.25)
|
JUMP_ANIMATION.play("jump", 0.25)
|
||||||
velocity.y += jump_velocity
|
velocity.y += jump_velocity # Adding instead of setting so jumping on slopes works properly
|
||||||
else:
|
else:
|
||||||
if Input.is_action_just_pressed(JUMP) and is_on_floor() and !low_ceiling:
|
if Input.is_action_just_pressed(JUMP) and is_on_floor() and !low_ceiling:
|
||||||
if jump_animation:
|
if jump_animation:
|
||||||
@ -298,6 +313,7 @@ func headbob_animation(moving):
|
|||||||
# This code is extremely performant but it makes no sense.
|
# This code is extremely performant but it makes no sense.
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
if HEADBOB_ANIMATION.is_playing():
|
||||||
HEADBOB_ANIMATION.play("RESET", 0.25)
|
HEADBOB_ANIMATION.play("RESET", 0.25)
|
||||||
HEADBOB_ANIMATION.speed_scale = 1
|
HEADBOB_ANIMATION.speed_scale = 1
|
||||||
|
|
||||||
@ -311,11 +327,13 @@ func _process(delta):
|
|||||||
|
|
||||||
if pausing_enabled:
|
if pausing_enabled:
|
||||||
if Input.is_action_just_pressed(PAUSE):
|
if Input.is_action_just_pressed(PAUSE):
|
||||||
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
|
match Input.mouse_mode:
|
||||||
|
Input.MOUSE_MODE_CAPTURED:
|
||||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||||
elif Input.mouse_mode == Input.MOUSE_MODE_VISIBLE:
|
Input.MOUSE_MODE_VISIBLE:
|
||||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||||
|
|
||||||
|
|
||||||
HEAD.rotation.x = clamp(HEAD.rotation.x, deg_to_rad(-90), deg_to_rad(90))
|
HEAD.rotation.x = clamp(HEAD.rotation.x, deg_to_rad(-90), deg_to_rad(90))
|
||||||
|
|
||||||
# Uncomment if you want full controller support
|
# Uncomment if you want full controller support
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
[gd_scene load_steps=20 format=3 uid="uid://cc1m2a1obsyn4"]
|
[gd_scene load_steps=21 format=3 uid="uid://cc1m2a1obsyn4"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://addons/fpc/character.gd" id="1_0t4e8"]
|
[ext_resource type="Script" path="res://addons/fpc/character.gd" id="1_0t4e8"]
|
||||||
|
[ext_resource type="Script" path="res://addons/fpc/EditorModule.gd" id="3_v3ckk"]
|
||||||
[ext_resource type="Script" path="res://addons/fpc/debug.gd" id="3_x1wcc"]
|
[ext_resource type="Script" path="res://addons/fpc/debug.gd" id="3_x1wcc"]
|
||||||
|
|
||||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_kp17n"]
|
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_kp17n"]
|
||||||
@ -455,3 +456,6 @@ layout_mode = 2
|
|||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
||||||
shape = SubResource("SphereShape3D_k4wwl")
|
shape = SubResource("SphereShape3D_k4wwl")
|
||||||
target_position = Vector3(0, 0.5, 0)
|
target_position = Vector3(0, 0.5, 0)
|
||||||
|
|
||||||
|
[node name="EditorModule" type="Node" parent="."]
|
||||||
|
script = ExtResource("3_v3ckk")
|
||||||
|
@ -5,11 +5,11 @@ func _process(delta):
|
|||||||
if visible:
|
if visible:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
func add_property(title : String, value, order : int):
|
func add_property(title : String, value, order : int): # This can either be called once for a static property or called every frame for a dynamic property
|
||||||
var target
|
var target
|
||||||
target = $MarginContainer/VBoxContainer.find_child(title, true, false)
|
target = $MarginContainer/VBoxContainer.find_child(title, true, false) # I have no idea what true and false does here, the function should be more specific
|
||||||
if !target:
|
if !target:
|
||||||
target = Label.new()
|
target = Label.new() # Debug lines are of type Label
|
||||||
$MarginContainer/VBoxContainer.add_child(target)
|
$MarginContainer/VBoxContainer.add_child(target)
|
||||||
target.name = title
|
target.name = title
|
||||||
target.text = title + ": " + str(value)
|
target.text = title + ": " + str(value)
|
||||||
|
@ -65,6 +65,9 @@ uv1_triplanar_sharpness = 0.000850145
|
|||||||
[node name="Character" parent="." instance=ExtResource("1_e18vq")]
|
[node name="Character" parent="." instance=ExtResource("1_e18vq")]
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
||||||
|
|
||||||
|
[node name="Camera" parent="Character/Head" index="0"]
|
||||||
|
transform = Transform3D(1, 0, 0, 0, 0.999391, -0.0348995, 0, 0.0348995, 0.999391, 0, 0, 0)
|
||||||
|
|
||||||
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
|
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
|
||||||
environment = SubResource("Environment_20rw3")
|
environment = SubResource("Environment_20rw3")
|
||||||
|
|
||||||
@ -104,3 +107,5 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -4.5, 3, -15.5)
|
|||||||
use_collision = true
|
use_collision = true
|
||||||
size = Vector3(19, 8, 1)
|
size = Vector3(19, 8, 1)
|
||||||
material = SubResource("StandardMaterial3D_7j4uu")
|
material = SubResource("StandardMaterial3D_7j4uu")
|
||||||
|
|
||||||
|
[editable path="Character"]
|
||||||
|
Reference in New Issue
Block a user