- Add soft-repulsion to prevent agent crowding - Implement dynamic Z-sorting based on world position - Add social orientation (agents face nearby agents) - Use velocity-based animation instead of isMoving flag - Track lastPosition for smooth velocity calculation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
162 lines
5.5 KiB
C#
162 lines
5.5 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
namespace TheIsland
|
|
{
|
|
/// <summary>
|
|
/// Procedural 2D animator for agents.
|
|
/// Handles idle breathing, movement bopping, and action-based squash/stretch.
|
|
/// Phase 19-E: Added banking turns and anticipation/overshoot.
|
|
/// </summary>
|
|
public class AgentAnimator : MonoBehaviour
|
|
{
|
|
[Header("Animation Settings")]
|
|
public float idleSpeed = 2f;
|
|
public float idleAmount = 0.04f;
|
|
public float moveBopSpeed = 12f;
|
|
public float moveBopAmount = 0.1f;
|
|
public float moveTiltAmount = 10f;
|
|
public float bankingAmount = 15f;
|
|
|
|
private SpriteRenderer _spriteRenderer;
|
|
private Vector3 _originalScale;
|
|
private Vector3 _targetLocalPos;
|
|
private Quaternion _targetLocalRot;
|
|
private Vector3 _targetScale;
|
|
|
|
private Vector3 _currentVelocity;
|
|
private float _velocityPercentage; // 0 to 1
|
|
private bool _isMoving;
|
|
private float _transitionTimer;
|
|
|
|
private void Awake()
|
|
{
|
|
_spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (_spriteRenderer != null)
|
|
{
|
|
_originalScale = _spriteRenderer.transform.localScale;
|
|
}
|
|
else
|
|
{
|
|
_originalScale = Vector3.one;
|
|
}
|
|
_targetScale = _originalScale;
|
|
}
|
|
|
|
public void SetMovement(Vector3 velocity, float maxVelocity = 3f)
|
|
{
|
|
_currentVelocity = velocity;
|
|
_velocityPercentage = Mathf.Clamp01(velocity.magnitude / Mathf.Max(0.1f, maxVelocity));
|
|
|
|
bool nowMoving = _velocityPercentage > 0.05f;
|
|
if (nowMoving && !_isMoving)
|
|
{
|
|
// Anticipation: Squash when starting to move
|
|
TriggerAnticipation();
|
|
}
|
|
else if (!nowMoving && _isMoving)
|
|
{
|
|
// Overshoot: Rebound when stopping
|
|
TriggerOvershoot();
|
|
}
|
|
|
|
_isMoving = nowMoving;
|
|
}
|
|
|
|
// Compatibility for older code
|
|
public void SetMovement(float currentVelocity, float maxVelocity)
|
|
{
|
|
SetMovement(new Vector3(currentVelocity, 0, 0), maxVelocity);
|
|
}
|
|
|
|
public void TriggerActionEffect()
|
|
{
|
|
StopAllCoroutines();
|
|
StartCoroutine(ActionPulseRoutine(0.4f, 1.3f));
|
|
}
|
|
|
|
private void TriggerAnticipation()
|
|
{
|
|
StartCoroutine(ActionPulseRoutine(0.15f, 0.8f)); // Squash
|
|
}
|
|
|
|
private void TriggerOvershoot()
|
|
{
|
|
StartCoroutine(ActionPulseRoutine(0.2f, 1.15f)); // Slight stretch
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_spriteRenderer == null) return;
|
|
|
|
if (_isMoving)
|
|
{
|
|
AnimateMove();
|
|
}
|
|
else
|
|
{
|
|
AnimateIdle();
|
|
}
|
|
|
|
// Smoothly apply transforms
|
|
float lerpSpeed = 12f;
|
|
var t = _spriteRenderer.transform;
|
|
t.localPosition = Vector3.Lerp(t.localPosition, _targetLocalPos, Time.deltaTime * lerpSpeed);
|
|
t.localRotation = Quaternion.Slerp(t.localRotation, _targetLocalRot, Time.deltaTime * lerpSpeed);
|
|
t.localScale = Vector3.Lerp(t.localScale, _targetScale, Time.deltaTime * lerpSpeed);
|
|
}
|
|
|
|
private void AnimateIdle()
|
|
{
|
|
// Idle "Breathing"
|
|
float breathe = Mathf.Sin(Time.time * idleSpeed) * idleAmount;
|
|
|
|
_targetScale = new Vector3(_originalScale.x, _originalScale.y * (1f + breathe), _originalScale.z);
|
|
_targetLocalPos = Vector3.zero;
|
|
_targetLocalRot = Quaternion.identity;
|
|
}
|
|
|
|
private void AnimateMove()
|
|
{
|
|
// Movement "Bopping" - Sin wave for vertical bounce
|
|
float cycle = Time.time * moveBopSpeed;
|
|
float bop = Mathf.Abs(Mathf.Sin(cycle)) * moveBopAmount * _velocityPercentage;
|
|
|
|
// Traditional Bop Tilt
|
|
float bopTilt = Mathf.Sin(cycle) * moveTiltAmount * _velocityPercentage;
|
|
|
|
// Phase 19-E: Banking Turn Tilt
|
|
// Lean into the direction of X velocity
|
|
float bankingTilt = -(_currentVelocity.x / 3f) * bankingAmount;
|
|
|
|
_targetLocalPos = new Vector3(0, bop, 0);
|
|
_targetLocalRot = Quaternion.Euler(0, 0, bopTilt + bankingTilt);
|
|
|
|
// Squash and stretch during the bop
|
|
float stretch = 1f + bop;
|
|
float squash = 1f / stretch;
|
|
_targetScale = new Vector3(_originalScale.x * squash, _originalScale.y * stretch, _originalScale.z);
|
|
}
|
|
|
|
private IEnumerator ActionPulseRoutine(float duration, float targetScaleY)
|
|
{
|
|
float elapsed = 0;
|
|
Vector3 peakScale = new Vector3(_originalScale.x * (2f - targetScaleY), _originalScale.y * targetScaleY, _originalScale.z);
|
|
|
|
while (elapsed < duration)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
float progress = elapsed / duration;
|
|
float sin = Mathf.Sin(progress * Mathf.PI);
|
|
|
|
// Temp override of targetScale for the pulse
|
|
_spriteRenderer.transform.localScale = Vector3.Lerp(_originalScale, peakScale, sin);
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|
|
} |