Files
AIGame/AIClient/Assets/HotUpdate/Scripts/Gameplay/ExplorerLocomotion.cs
T

35 lines
1.1 KiB
C#

using UnityEngine;
namespace HotUpdate.Gameplay
{
/// <summary>Plays Idle or Walk on the camp guide from how far the root moved.</summary>
public sealed class ExplorerLocomotion : MonoBehaviour
{
Animator _animator;
bool _hasMotion;
Vector3 _last;
void Awake()
{
_animator = GetComponentInChildren<Animator>();
if (_animator != null)
{
_animator.applyRootMotion = false;
foreach (var parameter in _animator.parameters)
if (parameter.name == "Motion" && parameter.type == AnimatorControllerParameterType.Int)
_hasMotion = true;
}
_last = transform.position;
}
void LateUpdate()
{
if (_animator == null || !_hasMotion) return;
Vector3 delta = transform.position - _last;
_last = transform.position;
float speed = new Vector2(delta.x, delta.z).magnitude / Mathf.Max(Time.deltaTime, 0.0001f);
_animator.SetInteger("Motion", speed > 0.04f ? 1 : 0);
}
}
}