Added a jump and land camera animation

This commit is contained in:
2024-01-19 20:31:04 -08:00
parent cc58b66cdd
commit 143a4e380d
2 changed files with 83 additions and 9 deletions

View File

@ -16,7 +16,8 @@ extends CharacterBody3D
@export_group("Nodes") @export_group("Nodes")
@export var HEAD : Node3D @export var HEAD : Node3D
@export var CAMERA : Camera3D @export var CAMERA : Camera3D
@export var CAMERA_ANIMATION : AnimationPlayer @export var HEADBOB_ANIMATION : AnimationPlayer
@export var JUMP_ANIMATION : AnimationPlayer
@export var COLLISION_MESH : CollisionShape3D @export var COLLISION_MESH : CollisionShape3D
@export_group("Controls") @export_group("Controls")
@ -48,12 +49,14 @@ extends CharacterBody3D
@export var dynamic_fov : bool = true @export var dynamic_fov : bool = true
@export var continuous_jumping : bool = true @export var continuous_jumping : bool = true
@export var view_bobbing : bool = true @export var view_bobbing : bool = true
@export var jump_animation : bool = true
# Member variables # Member variables
var speed : float = base_speed var speed : float = base_speed
# 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
# 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
var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity") # Don't set this as a const, see the gravity section in _physics_process 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,7 +70,8 @@ func _ready():
HEAD.set_rotation_degrees(initial_facing_direction) # I don't want to be calling this function if the vector is zero HEAD.set_rotation_degrees(initial_facing_direction) # I don't want to be calling this function if the vector is zero
# Reset the camera position # Reset the camera position
CAMERA_ANIMATION.play("RESET") HEADBOB_ANIMATION.play("RESET")
JUMP_ANIMATION.play("RESET")
func _physics_process(delta): func _physics_process(delta):
@ -104,14 +108,24 @@ func _physics_process(delta):
if view_bobbing: if view_bobbing:
headbob_animation(input_dir) headbob_animation(input_dir)
if jump_animation:
if !was_on_floor and is_on_floor(): # Just landed
JUMP_ANIMATION.play("land")
was_on_floor = is_on_floor() # This must always be at the end of physics_process
func handle_jumping(): func handle_jumping():
if jumping_enabled: if jumping_enabled:
if continuous_jumping: if continuous_jumping:
if Input.is_action_pressed(JUMP) and is_on_floor(): if Input.is_action_pressed(JUMP) and is_on_floor():
if jump_animation:
JUMP_ANIMATION.play("jump")
velocity.y += jump_velocity velocity.y += jump_velocity
else: else:
if Input.is_action_just_pressed(JUMP) and is_on_floor(): if Input.is_action_just_pressed(JUMP) and is_on_floor():
if jump_animation:
JUMP_ANIMATION.play("jump")
velocity.y += jump_velocity velocity.y += jump_velocity
@ -214,10 +228,10 @@ func update_collision_scale():
func headbob_animation(moving): func headbob_animation(moving):
if moving and is_on_floor(): if moving and is_on_floor():
CAMERA_ANIMATION.play("headbob", 0.25) HEADBOB_ANIMATION.play("headbob", 0.25)
CAMERA_ANIMATION.speed_scale = (speed / base_speed) * 1.75 HEADBOB_ANIMATION.speed_scale = (speed / base_speed) * 1.75
else: else:
CAMERA_ANIMATION.play("RESET", 0.25) HEADBOB_ANIMATION.play("RESET", 0.25)
func _process(delta): func _process(delta):

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=11 format=3 uid="uid://cc1m2a1obsyn4"] [gd_scene load_steps=15 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="PackedScene" uid="uid://3mij3cjhkwsm" path="res://addons/fpc/reticles/reticle_1.tscn" id="2_uuexm"] [ext_resource type="PackedScene" uid="uid://3mij3cjhkwsm" path="res://addons/fpc/reticles/reticle_1.tscn" id="2_uuexm"]
@ -96,17 +96,70 @@ _data = {
"headbob": SubResource("Animation_lrqmv") "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"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Camera:rotation")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector3(0.0349066, 0, 0)]
}
[sub_resource type="Animation" id="Animation_vsknp"]
resource_name = "land"
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="AnimationLibrary" id="AnimationLibrary_qeg5r"]
_data = {
"RESET": SubResource("Animation_fvvjq"),
"jump": SubResource("Animation_s07ye"),
"land": SubResource("Animation_vsknp")
}
[sub_resource type="Theme" id="Theme_wdf0f"] [sub_resource type="Theme" id="Theme_wdf0f"]
MarginContainer/constants/margin_bottom = 10 MarginContainer/constants/margin_bottom = 10
MarginContainer/constants/margin_left = 10 MarginContainer/constants/margin_left = 10
MarginContainer/constants/margin_right = 10 MarginContainer/constants/margin_right = 10
MarginContainer/constants/margin_top = 10 MarginContainer/constants/margin_top = 10
[node name="Character" type="CharacterBody3D" node_paths=PackedStringArray("HEAD", "CAMERA", "CAMERA_ANIMATION", "COLLISION_MESH")] [node name="Character" type="CharacterBody3D" node_paths=PackedStringArray("HEAD", "CAMERA", "HEADBOB_ANIMATION", "JUMP_ANIMATION", "COLLISION_MESH")]
script = ExtResource("1_0t4e8") script = ExtResource("1_0t4e8")
HEAD = NodePath("Head") HEAD = NodePath("Head")
CAMERA = NodePath("Head/Camera") CAMERA = NodePath("Head/Camera")
CAMERA_ANIMATION = NodePath("Head/camera_animation") HEADBOB_ANIMATION = NodePath("Head/HeadbobAnimation")
JUMP_ANIMATION = NodePath("Head/JumpAnimation")
COLLISION_MESH = NodePath("Collision") COLLISION_MESH = NodePath("Collision")
CROUCH = "crouch" CROUCH = "crouch"
SPRINT = "sprint" SPRINT = "sprint"
@ -123,13 +176,20 @@ shape = SubResource("CapsuleShape3D_uy03j")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0)
[node name="Camera" type="Camera3D" parent="Head"] [node name="Camera" type="Camera3D" parent="Head"]
transform = Transform3D(1, 0, 0, 0, 0.999391, -0.0348995, 0, 0.0348995, 0.999391, 0, 0, 0)
[node name="camera_animation" type="AnimationPlayer" parent="Head"] [node name="HeadbobAnimation" type="AnimationPlayer" parent="Head"]
libraries = { libraries = {
"": SubResource("AnimationLibrary_o0unb") "": SubResource("AnimationLibrary_o0unb")
} }
blend_times = [&"RESET", &"RESET", 0.5, &"RESET", &"headbob", 0.5, &"headbob", &"RESET", 0.5] blend_times = [&"RESET", &"RESET", 0.5, &"RESET", &"headbob", 0.5, &"headbob", &"RESET", 0.5]
[node name="JumpAnimation" type="AnimationPlayer" parent="Head"]
libraries = {
"": SubResource("AnimationLibrary_qeg5r")
}
speed_scale = 4.0
[node name="UserInterface" type="Control" parent="."] [node name="UserInterface" type="Control" parent="."]
layout_mode = 3 layout_mode = 3
anchors_preset = 15 anchors_preset = 15