Improved animations

This commit is contained in:
Michel 2025-02-01 12:53:22 +01:00
parent ab3890896d
commit cf8ba8bacb
34 changed files with 25503 additions and 4869 deletions

View file

@ -1,16 +1,31 @@
extends CharacterBody3D
@onready var animationtree: AnimationTree = $Camera3D/Weapon/AnimationPlayer/AnimationTree
class_name Player
enum PlayerStates {
STANDING,
CROUCHING,
SPRINTING
}
static var Instance: Player;
@onready var animationtree: AnimationTree = $CollisionShape3D/Camera3D/Weapon/AnimationPlayer/AnimationTree
@onready var collisionShape: CollisionShape3D = $CollisionShape3D
const SPEED: float = 5.0
const SPRINT_SPEED: float = 10.0
const FALLOFF_SPEED: float = 0.5
const JUMP_VELOCITY: float = 4.5
var lastDirection = Vector2(0,0)
@export var PlayerState: PlayerStates = PlayerStates.STANDING
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready() -> void:
Instance = self
func _physics_process(delta: float) -> void:
# Add the gravity.
@ -30,15 +45,42 @@ func _physics_process(delta: float) -> void:
if not is_on_floor():
falloffSpeed = 0.02;
var speed = SPEED;
self.handleCrouching()
if PlayerState == PlayerStates.STANDING && Input.is_action_pressed('player_sprint'):
self.PlayerState = PlayerStates.SPRINTING
if PlayerState == PlayerStates.SPRINTING && Input.is_action_just_released('player_sprint'):
self.PlayerState = PlayerStates.STANDING
if PlayerState == PlayerStates.SPRINTING:
speed = SPRINT_SPEED
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
velocity.x = direction.x * speed
velocity.z = direction.z * speed
else:
velocity.x = move_toward(velocity.x, 0, falloffSpeed)
velocity.z = move_toward(velocity.z, 0, falloffSpeed)
move_and_slide()
if self.PlayerState == PlayerStates.SPRINTING:
input_dir.y *= 2
var blendProperty = 'parameters/Movement/blend_position'
if PlayerState == PlayerStates.CROUCHING:
blendProperty = 'parameters/Crouched Movement/blend_position'
lastDirection.x = move_toward(lastDirection.x, input_dir.x, 0.05);
lastDirection.y = move_toward(lastDirection.y, input_dir.y, 0.05);
animationtree.set('parameters/Movement/blend_position', lastDirection);
animationtree.set(blendProperty, lastDirection);
func handleCrouching():
if Input.is_action_just_pressed('player_crouching'):
self.PlayerState = PlayerStates.CROUCHING
(collisionShape.shape as CylinderShape3D).height = 1
self.animationtree['parameters/playback'].travel('Crouched Movement')
if Input.is_action_just_released('player_crouching'):
self.PlayerState = PlayerStates.STANDING
(collisionShape.shape as CylinderShape3D).height = 2
self.animationtree['parameters/playback'].travel('Movement')

View file

@ -19,7 +19,11 @@ func _input(event: InputEvent) -> void:
if self.timer.time_left > 0:
return
self.animationtree.set('parameters/Fire/blend_position', randf_range(0, 2))
self.statemachine.travel('Fire')
var travelTarget = 'Fire'
if Player.Instance.PlayerState == Player.PlayerStates.CROUCHING:
travelTarget = 'Crouched Fire'
self.animationtree.set('parameters/' + travelTarget + '/blend_position', randf_range(0, 2))
self.statemachine.travel(travelTarget)
self.timer.start()