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>

View File

@@ -28,6 +28,9 @@ public class FollowerController: MonoBehaviour
private float _timer;
private bool _isManualFollowing = true;
private Vector3 _lastMoveDir = Vector3.right;
// Direction variables for 2D blend tree animation
private float _lastDirX = 0f; // -1 (left) to 1 (right)
private float _lastDirY = -1f; // -1 (down) to 1 (up)
private float _currentSpeed = 0f;
private Animator _animator;
private Transform _artTransform;
@@ -139,14 +142,6 @@ public class FollowerController: MonoBehaviour
_currentSpeed = Mathf.Max(_currentSpeed, minSpeed);
}
Vector3 dir = (_targetPoint - transform.position).normalized;
// Sprite flipping based on movement direction
if (_spriteRenderer != null && dir.sqrMagnitude > 0.001f)
{
if (dir.x > 0.01f)
_spriteRenderer.flipX = false;
else if (dir.x < -0.01f)
_spriteRenderer.flipX = true;
}
transform.position += dir * _currentSpeed * Time.deltaTime;
}
else
@@ -154,30 +149,56 @@ public class FollowerController: MonoBehaviour
_currentSpeed = 0f;
}
}
if (_isReturningToPlayer && _aiPath != null && _aiPath.enabled && _playerTransform != null)
{
_aiPath.destination = _playerTransform.position;
}
if (_animator != null)
{
float normalizedSpeed = 0f;
Vector3 velocity = Vector3.zero;
if (_isManualFollowing)
{
normalizedSpeed = _currentSpeed / _followerMaxSpeed;
// Calculate direction vector for manual movement
if (_currentSpeed > 0.01f)
{
velocity = (_targetPoint - transform.position).normalized * _currentSpeed;
}
}
else if (_aiPath != null)
{
normalizedSpeed = _aiPath.velocity.magnitude / _followerMaxSpeed;
// Sprite flipping for pathfinding mode
if (_spriteRenderer != null && _aiPath.velocity.sqrMagnitude > 0.001f)
{
if (_aiPath.velocity.x > 0.01f)
_spriteRenderer.flipX = false;
else if (_aiPath.velocity.x < -0.01f)
_spriteRenderer.flipX = true;
}
velocity = _aiPath.velocity;
}
// Set speed parameter for idle/walk transitions
_animator.SetFloat("Speed", Mathf.Clamp01(normalizedSpeed));
// 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);
}
}
}