Crouch fixes

This commit is contained in:
2024-01-20 14:42:32 -08:00
parent e5295a7cc8
commit f06d382b3b
2 changed files with 133 additions and 29 deletions

View File

@ -18,6 +18,7 @@ extends CharacterBody3D
@export var CAMERA : Camera3D
@export var HEADBOB_ANIMATION : AnimationPlayer
@export var JUMP_ANIMATION : AnimationPlayer
@export var CROUCH_ANIMATION : AnimationPlayer
@export var COLLISION_MESH : CollisionShape3D
@export_group("Controls")
@ -103,7 +104,6 @@ func _physics_process(delta):
handle_state(input_dir)
if dynamic_fov:
update_camera_fov()
update_collision_scale()
if view_bobbing:
headbob_animation(input_dir)
@ -154,7 +154,7 @@ func handle_movement(delta, input_dir):
func handle_state(moving):
if sprint_enabled:
if sprint_mode == 0:
if Input.is_action_pressed(SPRINT) and !Input.is_action_pressed(CROUCH):
if Input.is_action_pressed(SPRINT) and state != "crouching":
if moving:
if state != "sprinting":
enter_sprint_state()
@ -196,6 +196,8 @@ func handle_state(moving):
func enter_normal_state():
#print("entering normal state")
var prev_state = state
if prev_state == "crouching":
CROUCH_ANIMATION.play_backwards("crouch")
state = "normal"
speed = base_speed
@ -204,10 +206,13 @@ func enter_crouch_state():
var prev_state = state
state = "crouching"
speed = crouch_speed
CROUCH_ANIMATION.play("crouch")
func enter_sprint_state():
#print("entering sprint state")
var prev_state = state
if prev_state == "crouching":
CROUCH_ANIMATION.play_backwards("crouch")
state = "sprinting"
speed = sprint_speed
@ -219,13 +224,6 @@ func update_camera_fov():
CAMERA.fov = lerp(CAMERA.fov, 75.0, 0.3)
func update_collision_scale():
if state == "crouching": # Add your own crouch animation code
COLLISION_MESH.scale.y = lerp(COLLISION_MESH.scale.y, 0.75, 0.2)
else:
COLLISION_MESH.scale.y = lerp(COLLISION_MESH.scale.y, 1.0, 0.2)
func headbob_animation(moving):
if moving and is_on_floor():
HEADBOB_ANIMATION.play("headbob", 0.25)

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=15 format=3 uid="uid://cc1m2a1obsyn4"]
[gd_scene load_steps=19 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"]
@ -14,6 +14,103 @@ material = SubResource("StandardMaterial3D_kp17n")
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_uy03j"]
[sub_resource type="Animation" id="Animation_5ec5e"]
resource_name = "crouch"
length = 0.2
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Mesh:scale")
tracks/0/interp = 2
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.2),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Vector3(1, 1, 1), Vector3(1, 0.75, 1)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Collision:scale")
tracks/1/interp = 2
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0, 0.2),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Vector3(1, 1, 1), Vector3(1, 0.75, 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, 0.2),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Vector3(0, 1, 0), Vector3(0, 0.75, 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, 0.2),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Vector3(0, 1, 0), Vector3(0, 0.75, 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, 0.2),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Vector3(0, 1.5, 0), Vector3(0, 1.12508, 0)]
}
[sub_resource type="Animation" id="Animation_pqev3"]
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/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(0, 0.75, 0)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Head:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(0, 1.5, 0)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_5e5t5"]
_data = {
"RESET": SubResource("Animation_pqev3"),
"crouch": SubResource("Animation_5ec5e")
}
[sub_resource type="Animation" id="Animation_gh776"]
resource_name = "RESET"
length = 0.001
@ -96,21 +193,6 @@ _data = {
"headbob": SubResource("Animation_lrqmv")
}
[sub_resource type="Animation" id="Animation_s07ye"]
resource_name = "jump"
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),
"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_fvvjq"]
length = 0.001
tracks/0/type = "value"
@ -126,6 +208,21 @@ tracks/0/keys = {
"values": [Vector3(0.0349066, 0, 0)]
}
[sub_resource type="Animation" id="Animation_s07ye"]
resource_name = "jump"
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),
"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"
tracks/0/type = "value"
@ -154,24 +251,32 @@ MarginContainer/constants/margin_left = 10
MarginContainer/constants/margin_right = 10
MarginContainer/constants/margin_top = 10
[node name="Character" type="CharacterBody3D" node_paths=PackedStringArray("HEAD", "CAMERA", "HEADBOB_ANIMATION", "JUMP_ANIMATION", "COLLISION_MESH")]
[sub_resource type="SphereShape3D" id="SphereShape3D_k4wwl"]
[node name="Character" type="CharacterBody3D" node_paths=PackedStringArray("HEAD", "CAMERA", "HEADBOB_ANIMATION", "JUMP_ANIMATION", "CROUCH_ANIMATION", "COLLISION_MESH")]
script = ExtResource("1_0t4e8")
HEAD = NodePath("Head")
CAMERA = NodePath("Head/Camera")
HEADBOB_ANIMATION = NodePath("Head/HeadbobAnimation")
JUMP_ANIMATION = NodePath("Head/JumpAnimation")
CROUCH_ANIMATION = NodePath("CrouchAnimation")
COLLISION_MESH = NodePath("Collision")
CROUCH = "crouch"
SPRINT = "sprint"
[node name="Mesh" type="MeshInstance3D" parent="."]
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, 0.75, 0)
mesh = SubResource("CapsuleMesh_jw1de")
[node name="Collision" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = SubResource("CapsuleShape3D_uy03j")
[node name="CrouchAnimation" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_5e5t5")
}
[node name="Head" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0)
@ -220,8 +325,9 @@ layout_mode = 2
[node name="VBoxContainer" type="VBoxContainer" parent="UserInterface/DebugPanel/MarginContainer"]
layout_mode = 2
[node name="CrouchCeilingDetection" type="RayCast3D" parent="."]
[node name="CrouchCeilingDetection" type="ShapeCast3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
target_position = Vector3(0, 1, 0)
shape = SubResource("SphereShape3D_k4wwl")
target_position = Vector3(0, 0.5, 0)
[editable path="UserInterface/Reticle_1"]