From 78aafa45f30ef051fae3eac23aaea1e9fe6c478c Mon Sep 17 00:00:00 2001 From: Nathan Lovato <12694995+NathanLovato@users.noreply.github.com> Date: Sat, 17 Aug 2024 16:18:15 +0200 Subject: [PATCH] fix AnimationPlayer blending issue in Godot 4.3 I made two sets of changes: - Not calling AnimationPlayer.play() every frame. It was causing the animation blending to try to apply/retrigger the animation somehow. - Changed the character model import settings to mark idle and walk animations as looping --- objects/character.tscn | 1 + objects/player.tscn | 15 ------- scripts/player.gd | 89 +++++++++++++++++++++--------------------- 3 files changed, 46 insertions(+), 59 deletions(-) diff --git a/objects/character.tscn b/objects/character.tscn index 27919ce..be5e43c 100644 --- a/objects/character.tscn +++ b/objects/character.tscn @@ -24,3 +24,4 @@ transform = Transform3D(1, 0, 0, 0, 0.999999, 0, 0, 0, 0.999999, 0, 0.6, 0) [node name="AnimationPlayer" parent="." index="1"] deterministic = true +playback_default_blend_time = 0.2 diff --git a/objects/player.tscn b/objects/player.tscn index 6b4b2ac..570bd35 100644 --- a/objects/player.tscn +++ b/objects/player.tscn @@ -33,21 +33,6 @@ shape = SubResource("CapsuleShape3D_gdq8c") [node name="Character" parent="." instance=ExtResource("2_nero3")] -[node name="leg-left" parent="Character/character/root" index="0"] -transform = Transform3D(0.965926, 0, 0.258819, 0, 1, 0, -0.258819, 0, 0.965926, 0.125, 0.17625, -0.02375) - -[node name="leg-right" parent="Character/character/root" index="1"] -transform = Transform3D(0.965926, 0, -0.258819, 0, 1, 0, 0.258819, 0, 0.965926, -0.125, 0.17625, -0.02375) - -[node name="torso" parent="Character/character/root" index="2"] -transform = Transform3D(1, 0, 0, 0, 0.996194, 0.0871557, 0, -0.0871557, 0.996194, -1.80478e-15, 0.17625, -0.02375) - -[node name="arm-left" parent="Character/character/root/torso" index="0"] -transform = Transform3D(0.707107, 0.707107, 0, -0.707107, 0.707107, 0, 0, 0, 1, 0.3, 0.175, 0) - -[node name="arm-right" parent="Character/character/root/torso" index="1"] -transform = Transform3D(0.707107, -0.707107, 0, 0.707107, 0.707107, 0, 0, 0, 1, -0.3, 0.1195, 0) - [node name="Shadow" type="Decal" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.9, 0) size = Vector3(1, 2, 1) diff --git a/scripts/player.gd b/scripts/player.gd index 2c8e00a..7dc3d12 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -28,119 +28,120 @@ var coins = 0 # Functions func _physics_process(delta): - + # Handle functions - + handle_controls(delta) handle_gravity(delta) - + handle_effects(delta) - + # Movement var applied_velocity: Vector3 - + applied_velocity = velocity.lerp(movement_velocity, delta * 10) applied_velocity.y = -gravity - + velocity = applied_velocity move_and_slide() - + # Rotation - + if Vector2(velocity.z, velocity.x).length() > 0: rotation_direction = Vector2(velocity.z, velocity.x).angle() - + rotation.y = lerp_angle(rotation.y, rotation_direction, delta * 10) - + # Falling/respawning - + if position.y < -10: get_tree().reload_current_scene() - + # Animation for scale (jumping and landing) - + model.scale = model.scale.lerp(Vector3(1, 1, 1), delta * 10) - + # Animation when landing - + if is_on_floor() and gravity > 2 and !previously_floored: model.scale = Vector3(1.25, 0.75, 1.25) Audio.play("res://sounds/land.ogg") - + previously_floored = is_on_floor() # Handle animation(s) func handle_effects(delta): - + particles_trail.emitting = false sound_footsteps.stream_paused = true - + if is_on_floor(): var horizontal_velocity = Vector2(velocity.x, velocity.z) var speed_factor = horizontal_velocity.length() / movement_speed / delta if speed_factor > 0.05: - animation.play("walk", 0.5, speed_factor) - + if animation.current_animation != "walk": + animation.play("walk") + if speed_factor > 0.3: sound_footsteps.stream_paused = false sound_footsteps.pitch_scale = speed_factor - + if speed_factor > 0.75: particles_trail.emitting = true - - else: - animation.play("idle", 0.5) - else: - animation.play("jump", 0.5) + + elif animation.current_animation != "idle": + animation.play("idle") + elif animation.current_animation != "jump": + animation.play("jump") # Handle movement input func handle_controls(delta): - + # Movement - + var input := Vector3.ZERO - + input.x = Input.get_axis("move_left", "move_right") input.z = Input.get_axis("move_forward", "move_back") - + input = input.rotated(Vector3.UP, view.rotation.y) - + if input.length() > 1: input = input.normalized() - + movement_velocity = input * movement_speed * delta - + # Jumping - + if Input.is_action_just_pressed("jump"): - + if jump_single or jump_double: jump() # Handle gravity func handle_gravity(delta): - + gravity += 25 * delta - + if gravity > 0 and is_on_floor(): - + jump_single = true gravity = 0 # Jumping func jump(): - + Audio.play("res://sounds/jump.ogg") - + gravity = -jump_strength - + model.scale = Vector3(0.5, 1.5, 0.5) - + if jump_single: jump_single = false; jump_double = true; @@ -150,7 +151,7 @@ func jump(): # Collecting coins func collect_coin(): - + coins += 1 - + coin_collected.emit(coins)