Example #1
0
        private void MovementUpdate(float deltaTime, out Vector3 nextPosition, out Quaternion nextRotation)
        {
            m_LastDeltatime = deltaTime;

            float currentAcceleration = MaxAcceleration;

            if (currentAcceleration < 0)
            {
                currentAcceleration *= -MaxSpeed;
            }

            if (UpdatePosition)
            {
                m_SimulatedPosition = transform.position;
            }
            if (UpdateRotation)
            {
                m_SimulatedRotation = transform.rotation;
            }

            var currentPosition = m_SimulatedPosition;

            interpolator.MoveToCircleIntersection(currentPosition, LookAheadDis);
            var dir = (SteerPosition - currentPosition);

            float distanceToEnd = dir.magnitude + Mathf.Max(0, interpolator.RemainingDistance);

            var prevTargetReached = ReachedEndOfPath;

            ReachedEndOfPath = distanceToEnd <= EndReachedDis && interpolator.Valid;
            if (!prevTargetReached && ReachedEndOfPath)
            {
                OnTargetReached();
            }

            float slowDown;

            var forward = m_SimulatedRotation * Vector3.forward;

            if (interpolator.Valid && !IsStopped)
            {
                slowDown = distanceToEnd < SlowDownDis?Mathf.Sqrt(distanceToEnd / SlowDownDis) : 1;

                //Todo: Get end of path mode
                if (ReachedEndOfPath)
                {
                    Velocity -= Vector3.ClampMagnitude(Velocity, currentAcceleration * deltaTime);
                }
                else
                {
                    Velocity += MovementUtils.CalculateAccelerationToReachPoint(dir, dir.normalized * MaxSpeed, Velocity,
                                                                                currentAcceleration, RotationSpeed, MaxSpeed, forward) * deltaTime;
                }
            }
            else
            {
                slowDown  = 1;
                Velocity -= Vector3.ClampMagnitude(Velocity, currentAcceleration * deltaTime);
            }

            Velocity = MovementUtils.ClampVelocity(Velocity, MaxSpeed, slowDown, SlowWhenNotFacingTarget, forward);

            //Todo: RVO Controller

            var delta = m_LastDeltaPosition = CalculateDeltaToMoveThisFrame(distanceToEnd, deltaTime);

            nextPosition = currentPosition + delta;

            CalculateNextRotation(slowDown, out nextRotation);
        }