示例#1
0
    private IEnumerator FollowPathRoutine(CurvePath path, float duration, Action callback)
    {
        _animator.applyRootMotion = true;

        float t = 0;

        while (t <= 1)
        {
            Vector3 prevPos = transform.localPosition;
            transform.position = path.EvaluatePosition(t);
            transform.rotation = path.EvaluateRotation(t);

            Vector3 moveDirection = transform.localPosition - prevPos;
            Vector3 lookDirection = transform.forward;

            // Animation
            ResetMovementAnimation();

            if (Vector3.Angle(lookDirection, moveDirection) < 90)
            {
                _animator.SetBool("IsMovingForward", true);
            }
            else
            {
                _animator.SetBool("IsMovingBackward", true);
            }

            t += Time.deltaTime / duration;

            yield return(null);
        }

        // Reset animation
        _animator.SetBool("IsMovingForward", false);
        _animator.SetBool("IsMovingBackward", false);

        _animator.applyRootMotion = false;

        callback?.Invoke();

        _followPathCoroutineInstance = null;
        _followingPath      = null;
        _followPathCallback = null;
    }
示例#2
0
    public void StopFollowingPath()
    {
        if (_followPathCoroutineInstance == null)
        {
            return;
        }

        StopCoroutine(_followPathCoroutineInstance);
        _followPathCoroutineInstance = null;

        // Reset position & rotation
        transform.position = _followingPath.EvaluatePosition(1);
        transform.rotation = _followingPath.EvaluateRotation(1);

        // Reset Animator
        ResetMovementAnimation();

        if (_followPathCallback != null)
        {
            _followPathCallback();
        }
    }
示例#3
0
 // Start is called before the first frame update
 void Start()
 {
     transform.position = path.EvaluatePosition(0);
 }