Last active
April 8, 2025 19:27
-
-
Save filipkrw/bed6cb058d578ac38a11fe8ad2601711 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends PlayerGD | |
@onready var mannequin = $Mannequin | |
@onready var animation_tree = $Mannequin/AnimationTree | |
var walk_target_blend_position = Vector2(0, 0) | |
func _process(delta: float) -> void: | |
animate(delta) | |
func animate(delta: float) -> void: | |
var velocity = get_velocity() | |
var horizontal_velocity = Vector3(velocity.x, 0, velocity.z) | |
# Rotate the mannequin to face the direction of movement & set the walk animation blend position | |
if horizontal_velocity.length() > 0: | |
var target_vector = horizontal_velocity.normalized() | |
var forward_vector = mannequin.transform.basis.z; | |
forward_vector.y = 0; | |
forward_vector = forward_vector.normalized(); | |
var dot = forward_vector.dot(target_vector); | |
dot = clamp(dot, -1.0, 1.0); | |
var angle_radians_abs = acos(dot); | |
var cross = forward_vector.cross(target_vector); | |
var angle_radians = angle_radians_abs if cross.y > 0 else -angle_radians_abs; | |
mannequin.rotate_y(angle_radians * delta * 15.0); | |
var speed = horizontal_velocity.length(); | |
var speed_factor = speed / 5.0; | |
walk_target_blend_position = Vector2(0, -speed_factor * 1.2); | |
else: | |
walk_target_blend_position = Vector2(0, 0); | |
var current_blend_position = animation_tree.get("parameters/BlendSpace2D/blend_position"); | |
animation_tree.set( | |
"parameters/BlendSpace2D/blend_position", | |
current_blend_position.lerp(walk_target_blend_position, 6.0 * delta) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment