From 143a4e380d3a031b06323a77a07761b2c011ed7a Mon Sep 17 00:00:00 2001 From: Zakarya Date: Fri, 19 Jan 2024 20:31:04 -0800 Subject: [PATCH] Added a jump and land camera animation --- addons/fpc/character.gd | 24 +++++++++++--- addons/fpc/character.tscn | 68 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/addons/fpc/character.gd b/addons/fpc/character.gd index 1cc1c97..ddc1e7e 100644 --- a/addons/fpc/character.gd +++ b/addons/fpc/character.gd @@ -16,7 +16,8 @@ extends CharacterBody3D @export_group("Nodes") @export var HEAD : Node3D @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_group("Controls") @@ -48,12 +49,14 @@ extends CharacterBody3D @export var dynamic_fov : bool = true @export var continuous_jumping : bool = true @export var view_bobbing : bool = true +@export var jump_animation : bool = true # Member variables var speed : float = base_speed # States: normal, crouching, sprinting 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 # 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,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 # Reset the camera position - CAMERA_ANIMATION.play("RESET") + HEADBOB_ANIMATION.play("RESET") + JUMP_ANIMATION.play("RESET") func _physics_process(delta): @@ -103,15 +107,25 @@ func _physics_process(delta): if view_bobbing: 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(): if jumping_enabled: if continuous_jumping: if Input.is_action_pressed(JUMP) and is_on_floor(): + if jump_animation: + JUMP_ANIMATION.play("jump") velocity.y += jump_velocity else: if Input.is_action_just_pressed(JUMP) and is_on_floor(): + if jump_animation: + JUMP_ANIMATION.play("jump") velocity.y += jump_velocity @@ -214,10 +228,10 @@ func update_collision_scale(): func headbob_animation(moving): if moving and is_on_floor(): - CAMERA_ANIMATION.play("headbob", 0.25) - CAMERA_ANIMATION.speed_scale = (speed / base_speed) * 1.75 + HEADBOB_ANIMATION.play("headbob", 0.25) + HEADBOB_ANIMATION.speed_scale = (speed / base_speed) * 1.75 else: - CAMERA_ANIMATION.play("RESET", 0.25) + HEADBOB_ANIMATION.play("RESET", 0.25) func _process(delta): diff --git a/addons/fpc/character.tscn b/addons/fpc/character.tscn index ec29731..22cf675 100644 --- a/addons/fpc/character.tscn +++ b/addons/fpc/character.tscn @@ -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="PackedScene" uid="uid://3mij3cjhkwsm" path="res://addons/fpc/reticles/reticle_1.tscn" id="2_uuexm"] @@ -96,17 +96,70 @@ _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" +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"] MarginContainer/constants/margin_bottom = 10 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", "CAMERA_ANIMATION", "COLLISION_MESH")] +[node name="Character" type="CharacterBody3D" node_paths=PackedStringArray("HEAD", "CAMERA", "HEADBOB_ANIMATION", "JUMP_ANIMATION", "COLLISION_MESH")] script = ExtResource("1_0t4e8") HEAD = NodePath("Head") CAMERA = NodePath("Head/Camera") -CAMERA_ANIMATION = NodePath("Head/camera_animation") +HEADBOB_ANIMATION = NodePath("Head/HeadbobAnimation") +JUMP_ANIMATION = NodePath("Head/JumpAnimation") COLLISION_MESH = NodePath("Collision") CROUCH = "crouch" 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) [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 = { "": SubResource("AnimationLibrary_o0unb") } 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="."] layout_mode = 3 anchors_preset = 15