Working directional movement animations

This commit is contained in:
Michal Pikulski
2025-09-29 15:55:25 +02:00
parent 532c532a1d
commit 5789dca302
21 changed files with 1610 additions and 332 deletions

View File

@@ -39,6 +39,10 @@ namespace Input
private Vector3 _lastDirectMoveDir = Vector3.right;
public Vector3 LastDirectMoveDir => _lastDirectMoveDir;
// --- Last movement directions for animation blend tree ---
private float _lastDirX = 0f; // -1 (left) to 1 (right)
private float _lastDirY = -1f; // -1 (down) to 1 (up)
// --- MoveToAndNotify State ---
public delegate void ArrivedAtTargetHandler();
private Coroutine moveToCoroutine;
@@ -238,6 +242,7 @@ namespace Input
{
float normalizedSpeed = 0f;
Vector3 velocity = Vector3.zero;
if (isHolding && GameManager.Instance.DefaultHoldMovementMode == HoldMovementMode.Direct)
{
normalizedSpeed = directMoveVelocity.magnitude / aiPath.maxSpeed;
@@ -248,8 +253,30 @@ namespace Input
normalizedSpeed = aiPath.velocity.magnitude / aiPath.maxSpeed;
velocity = aiPath.velocity;
}
// Set speed parameter as before
animator.SetFloat("Speed", Mathf.Clamp01(normalizedSpeed));
SetSpriteFlip(velocity);
// Calculate and set X and Y directions for 2D blend tree
if (velocity.sqrMagnitude > 0.01f)
{
// Normalize the velocity vector to get direction
Vector3 normalizedVelocity = velocity.normalized;
// Update the stored directions when actively moving
_lastDirX = normalizedVelocity.x;
_lastDirY = normalizedVelocity.y;
// Set the animator parameters
animator.SetFloat("DirX", _lastDirX);
animator.SetFloat("DirY", _lastDirY);
}
else
{
// When not moving, keep using the last direction
animator.SetFloat("DirX", _lastDirX);
animator.SetFloat("DirY", _lastDirY);
}
}
}
@@ -286,17 +313,6 @@ namespace Input
}
}
private void SetSpriteFlip(Vector3 velocity)
{
if (spriteRenderer != null && velocity.sqrMagnitude > 0.001f)
{
if (velocity.x > 0.01f)
spriteRenderer.flipX = false;
else if (velocity.x < -0.01f)
spriteRenderer.flipX = true;
}
}
/// <summary>
/// Coroutine for updating the AIPath destination during pathfinding hold movement.
/// </summary>