I have tried the solution with Unity Site, as suggested in the script:
https://docs.unity3d.com/Manual/nav-CouplingAnimationAndNavigation.html
but it didn’t work, and for some reason the rig is falling through the floor
The code
[SerializeField] private AIPath _ai;
void AsynceAnimationsWithPathFiniding()
{
Vector3 nextPositio;
Quaternion nextRotation;
_ai.MovementUpdate(Time.deltaTime, out nextPositio, out nextRotation);
Vector3 worldDeltaPosition = nextPositio - transform.position;
worldDeltaPosition.y = 0;
float dx = Vector3.Dot(transform.right, worldDeltaPosition);
float dy = Vector3.Dot(transform.forward, worldDeltaPosition);
Vector3 deltaPosition = new Vector2(dx, dy);
float smooth = Mathf.Min(1, Time.deltaTime / containeSpeed);
_smoothDeltaPosition = Vector2.Lerp(_smoothDeltaPosition, deltaPosition, smooth);
_velocity = _smoothDeltaPosition / Time.deltaTime;
var remaningDistance = _ai.remainingDistance;
var stopingDistance = _ai.endReachedDistance;
var raidus = _ai.radius;
if (remaningDistance <= stopingDistance)
{
_velocity = Vector2.Lerp(
Vector2.zero,
_velocity,
remaningDistance / stopingDistance
);
}
bool shouldMove = _velocity.magnitude > _minimumeVelocityToMove && remaningDistance > raidus;
var walkForawd = Mathf.Clamp(_velocity.y, -0.64f, 6f);
var turnSide = Mathf.Lerp(_animator.GetFloat("VelX"), _velocity.x, smooth);
var angle = Angle(nextPositio);
var howMuchToTurn = Mathf.InverseLerp(0, 90, angle);
var turnAngerl = Mathf.Lerp(0, 6, howMuchToTurn);
bool doesNeedTurn = Angle(nextPositio) > 85;
_animator.SetBool("Turn", doesNeedTurn);
_animator.SetBool("Move", shouldMove);
_animator.SetFloat("VelX", turnSide);
_animator.SetFloat("VelY", walkForawd);
float deltaMagnitude = worldDeltaPosition.magnitude;
if (deltaMagnitude > raidus / 2f)
{
transform.position = Vector3.Lerp(
_animator.rootPosition,
nextPositio,
smooth
);
}
}
float Angle(Vector3 nextPostion)
{
Vector3 targetDir = nextPostion - transform.position;
float angle = Vector3.Angle(targetDir, transform.forward);
return angle;
}
private void OnAnimatorMove()
{
_ai.canMove = false;
_ai.MovementUpdate(Time.deltaTime, out var nextPosition, out var nextRotation);
Vector3 rootPosition = _animator.rootPosition;
rootPosition.y = nextPosition.y;
transform.position = rootPosition;
transform.rotation = _animator.rootRotation;
_ai.FinalizeMovement(rootPosition, nextRotation);
}
Blockquote