Working directional movement animations
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user