private void LateUpdate()
        {
            float        t       = NormalizedT;
            BezierSpline spline  = Spline;
            bool         forward = MovingForward;

            for (int i = 0; i < tailObjects.Count; i++)
            {
                Transform tailObject = tailObjects[i];
                float     dt         = Time.deltaTime;

                if (forward)
                {
                    tailObject.position = spline.MoveAlongSpline(ref t, -tailObjectDistances[i]);

                    if (lookForward)
                    {
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(spline.GetTangent(t)), rotationLerpModifier * dt);
                    }
                }
                else
                {
                    tailObject.position = spline.MoveAlongSpline(ref t, tailObjectDistances[i]);

                    if (lookForward)
                    {
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(-spline.GetTangent(t)), rotationLerpModifier * dt);
                    }
                }
            }
        }
        public override void Execute(float deltaTime)
        {
            float        t       = NormalizedT;
            BezierSpline spline  = Spline;
            bool         forward = MovingForward;

            for (int i = 0; i < tailObjects.Count; i++)
            {
                Transform tailObject = tailObjects[i];

                if (forward)
                {
                    if (!tailObject)
                    {
                        return;
                    }
                    tailObject.position = Vector3.Lerp(tailObject.position, spline.MoveAlongSpline(ref t, -tailObjectDistances[i]), movementLerpModifier * deltaTime);

                    if (lookAt == LookAtMode.Forward)
                    {
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(spline.GetTangent(t)), rotationLerpModifier * deltaTime);
                    }
                    else if (lookAt == LookAtMode.SplineExtraData)
                    {
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, spline.GetExtraData(t, InterpolateExtraDataAsQuaternion), rotationLerpModifier * deltaTime);
                    }
                }
                else
                {
                    tailObject.position = Vector3.Lerp(tailObject.position, spline.MoveAlongSpline(ref t, tailObjectDistances[i]), movementLerpModifier * deltaTime);

                    if (lookAt == LookAtMode.Forward)
                    {
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(-spline.GetTangent(t)), rotationLerpModifier * deltaTime);
                    }
                    else if (lookAt == LookAtMode.SplineExtraData)
                    {
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, spline.GetExtraData(t, InterpolateExtraDataAsQuaternion), rotationLerpModifier * deltaTime);
                    }
                }
            }
        }
        public override void Execute(float deltaTime)
        {
            float        t       = NormalizedT;
            BezierSpline spline  = Spline;
            bool         forward = MovingForward;

            for (int i = 0; i < tailObjects.Count; i++)
            {
                Transform tailObject = tailObjects[i];

                if (forward)
                {
                    tailObject.position = Vector3.Lerp(tailObject.position, spline.MoveAlongSpline(ref t, -tailObjectDistances[i]), movementLerpModifier * deltaTime);

                    if (lookAt == LookAtMode.Forward)
                    {
                        BezierSpline.PointIndexTuple tuple = spline.GetNearestPointIndicesTo(t);
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(tuple.GetTangent(), tuple.GetNormal()), rotationLerpModifier * deltaTime);
                    }
                    else if (lookAt == LookAtMode.SplineExtraData)
                    {
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, spline.GetExtraData(t, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime);
                    }
                }
                else
                {
                    tailObject.position = Vector3.Lerp(tailObject.position, spline.MoveAlongSpline(ref t, tailObjectDistances[i]), movementLerpModifier * deltaTime);

                    if (lookAt == LookAtMode.Forward)
                    {
                        BezierSpline.PointIndexTuple tuple = spline.GetNearestPointIndicesTo(t);
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(-tuple.GetTangent(), tuple.GetNormal()), rotationLerpModifier * deltaTime);
                    }
                    else if (lookAt == LookAtMode.SplineExtraData)
                    {
                        tailObject.rotation = Quaternion.Lerp(tailObject.rotation, spline.GetExtraData(t, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime);
                    }
                }
            }
        }
Beispiel #4
0
        public override void Execute(float deltaTime)
        {
            BezierSpline spline  = Spline;
            float        t       = highQuality ? spline.evenlySpacedPoints.GetPercentageAtNormalizedT(NormalizedT) : NormalizedT;
            bool         forward = MovingForward;

            for (int i = 0; i < tailObjects.Count; i++)
            {
                Transform tailObject = tailObjects[i];
                Vector3   tailPosition;
                float     tailNormalizedT;
                if (highQuality)
                {
                    if (forward)
                    {
                        t -= tailObjectDistances[i] / spline.evenlySpacedPoints.splineLength;
                    }
                    else
                    {
                        t += tailObjectDistances[i] / spline.evenlySpacedPoints.splineLength;
                    }

                    tailNormalizedT = spline.evenlySpacedPoints.GetNormalizedTAtPercentage(t);
                    tailPosition    = spline.GetPoint(tailNormalizedT);
                }
                else
                {
                    tailPosition    = spline.MoveAlongSpline(ref t, forward ? -tailObjectDistances[i] : tailObjectDistances[i]);
                    tailNormalizedT = t;
                }

                tailObject.position = Vector3.Lerp(tailObject.position, tailPosition, movementLerpModifier * deltaTime);

                if (lookAt == LookAtMode.Forward)
                {
                    BezierSpline.Segment segment = spline.GetSegmentAt(tailNormalizedT);
                    tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(forward ? segment.GetTangent() : -segment.GetTangent(), segment.GetNormal()), rotationLerpModifier * deltaTime);
                }
                else if (lookAt == LookAtMode.SplineExtraData)
                {
                    tailObject.rotation = Quaternion.Lerp(tailObject.rotation, spline.GetExtraData(tailNormalizedT, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime);
                }
            }
        }
Beispiel #5
0
        private void PrepareCache()
        {
            if (!cacheCalculated)
            {
                return;
            }

            cacheCalculated = false;

            List <float> list    = new List <float>();
            float        percent = 0;
            float        detail  = 1 / cacheQuality;

            do
            {
                list.Add(percent);
                spline.MoveAlongSpline(ref percent, detail);
            }while (percent < 1);

            cache = list.ToArray();
            Debug.Log("completed caching with size of " + cache.Length);
        }
Beispiel #6
0
        public override void Execute(float deltaTime)
        {
            float targetSpeed = (isGoingForward) ? speed : -speed;

            Vector3 targetPos = spline.MoveAlongSpline(ref m_normalizedT, targetSpeed * deltaTime);

            transform.position = targetPos;
            //transform.position = Vector3.Lerp( transform.position, targetPos, movementLerpModifier * deltaTime );

            bool movingForward = MovingForward;

            if (lookAt == LookAtMode.Forward)
            {
                Quaternion targetRotation;
                if (movingForward)
                {
                    targetRotation = Quaternion.LookRotation(spline.GetTangent(m_normalizedT));
                }
                else
                {
                    targetRotation = Quaternion.LookRotation(-spline.GetTangent(m_normalizedT));
                }

                transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationLerpModifier * deltaTime);
            }
            else if (lookAt == LookAtMode.SplineExtraData)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, spline.GetExtraData(m_normalizedT, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime);
            }

            if (movingForward)
            {
                if (m_normalizedT >= 1f)
                {
                    if (travelMode == TravelMode.Once)
                    {
                        m_normalizedT = 1f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        m_normalizedT -= 1f;
                    }
                    else
                    {
                        m_normalizedT  = 2f - m_normalizedT;
                        isGoingForward = !isGoingForward;
                    }

                    if (!onPathCompletedCalledAt1)
                    {
                        onPathCompletedCalledAt1 = true;
#if UNITY_EDITOR
                        if (UnityEditor.EditorApplication.isPlaying)
#endif
                        onPathCompleted.Invoke();
                    }
                }
                else
                {
                    onPathCompletedCalledAt1 = false;
                }
            }
            else
            {
                if (m_normalizedT <= 0f)
                {
                    if (travelMode == TravelMode.Once)
                    {
                        m_normalizedT = 0f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        m_normalizedT += 1f;
                    }
                    else
                    {
                        m_normalizedT  = -m_normalizedT;
                        isGoingForward = !isGoingForward;
                    }

                    if (!onPathCompletedCalledAt0)
                    {
                        onPathCompletedCalledAt0 = true;
#if UNITY_EDITOR
                        if (UnityEditor.EditorApplication.isPlaying)
#endif
                        onPathCompleted.Invoke();
                    }
                }
                else
                {
                    onPathCompletedCalledAt0 = false;
                }
            }
        }
        private void Update()
        {
            float targetSpeed = (isGoingForward) ? speed : -speed;

            Vector3 targetPos = spline.MoveAlongSpline(ref progress, targetSpeed * Time.deltaTime);

            cachedTransform.position = targetPos;
            //cachedTransform.position = Vector3.Lerp( cachedTransform.position, targetPos, movementLerpModifier * Time.deltaTime );

            bool movingForward = MovingForward;

            if (lookForward)
            {
                Quaternion targetRotation;
                if (movingForward)
                {
                    targetRotation = Quaternion.LookRotation(spline.GetTangent(progress));
                }
                else
                {
                    targetRotation = Quaternion.LookRotation(-spline.GetTangent(progress));
                }

                cachedTransform.rotation = Quaternion.Lerp(cachedTransform.rotation, targetRotation, rotationLerpModifier * Time.deltaTime);
            }

            if (movingForward)
            {
                if (progress >= 1f)
                {
                    if (!onPathCompletedCalledAt1)
                    {
                        onPathCompleted.Invoke();
                        onPathCompletedCalledAt1 = true;
                    }

                    if (travelMode == TravelMode.Once)
                    {
                        progress = 1f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        progress -= 1f;
                    }
                    else
                    {
                        progress       = 2f - progress;
                        isGoingForward = !isGoingForward;
                    }
                }
                else
                {
                    onPathCompletedCalledAt1 = false;
                }
            }
            else
            {
                if (progress <= 0f)
                {
                    if (!onPathCompletedCalledAt0)
                    {
                        onPathCompleted.Invoke();
                        onPathCompletedCalledAt0 = true;
                    }

                    if (travelMode == TravelMode.Once)
                    {
                        progress = 0f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        progress += 1f;
                    }
                    else
                    {
                        progress       = -progress;
                        isGoingForward = !isGoingForward;
                    }
                }
                else
                {
                    onPathCompletedCalledAt0 = false;
                }
            }
        }
Beispiel #8
0
        void Update()
        {
            float targetSpeed = (isGoingForward) ? speed : -speed;

            Vector3 targetPos;

            // Code below uses the obsolete MoveAlongSpline function
            //float absSpeed = Mathf.Abs( speed );
            //if( absSpeed <= 2f )
            //	targetPos = spline.MoveAlongSpline( ref progress, targetSpeed * Time.deltaTime, maximumError: 0f );
            //else if( absSpeed >= 40f )
            //	targetPos = spline.MoveAlongSpline( ref progress, targetSpeed * Time.deltaTime, increasedAccuracy: true );
            //else
            //	targetPos = spline.MoveAlongSpline( ref progress, targetSpeed * Time.deltaTime );

            targetPos = spline.MoveAlongSpline(ref progress, targetSpeed * Time.deltaTime);

            cachedTransform.position = targetPos;
            //cachedTransform.position = Vector3.Lerp( cachedTransform.position, targetPos, movementLerpModifier * Time.deltaTime );

            bool movingForward = (speed > 0f) == isGoingForward;

            if (lookForward)
            {
                Quaternion targetRotation;
                if (movingForward)
                {
                    targetRotation = Quaternion.LookRotation(spline.GetTangent(progress));
                }
                else
                {
                    targetRotation = Quaternion.LookRotation(-spline.GetTangent(progress));
                }

                cachedTransform.rotation = Quaternion.Lerp(cachedTransform.rotation, targetRotation, rotationLerpModifier * Time.deltaTime);
            }

            if (movingForward)
            {
                if (progress >= 1f - relaxationAtEndPoints)
                {
                    if (!onPathCompletedCalledAt1)
                    {
                        onPathCompleted.Invoke();
                        onPathCompletedCalledAt1 = true;
                    }

                    if (travelMode == TravelMode.Once)
                    {
                        progress = 1f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        progress -= 1f;
                    }
                    else
                    {
                        progress       = 2f - progress;
                        isGoingForward = !isGoingForward;
                    }
                }
                else
                {
                    onPathCompletedCalledAt1 = false;
                }
            }
            else
            {
                if (progress <= relaxationAtEndPoints)
                {
                    if (!onPathCompletedCalledAt0)
                    {
                        onPathCompleted.Invoke();
                        onPathCompletedCalledAt0 = true;
                    }

                    if (travelMode == TravelMode.Once)
                    {
                        progress = 0f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        progress += 1f;
                    }
                    else
                    {
                        progress       = -progress;
                        isGoingForward = !isGoingForward;
                    }
                }
                else
                {
                    onPathCompletedCalledAt0 = false;
                }
            }
        }