Exemple #1
0
        //=========//
        // HELPERS //
        //=========//
        private void InterpolatePositionAndRotation(float percent)
        {
            float normalizedT = MapPercentToBeziereRatio(progress);

            if (smoothness == 0)
            {
                transform.position = spline.GetPoint(normalizedT);
                transform.rotation = Quaternion.LookRotation(spline.GetTangent(normalizedT));
            }
            else
            {
                transform.position = Vector3.Lerp(transform.position, spline.GetPoint(normalizedT), Time.deltaTime * 100 / smoothness);
                transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(spline.GetTangent(normalizedT)), Time.deltaTime * 100 / smoothness);
            }
        }
Exemple #2
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;
                }
            }
        }
        public override void Execute(float deltaTime)
        {
            if (curSpline != null)
            {
                transform.position = Vector3.Lerp(transform.position, curSpline.GetPoint(m_normalizedT), movementLerpModifier * deltaTime);

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

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

                if (isGoingForward)
                {
                    m_normalizedT += deltaTime / n_travelTime;

                    if (m_normalizedT > 1f)
                    {
                        if (!onPathCompletedCalledAt1)
                        {
                            onPathCompletedCalledAt1 = true;
#if UNITY_EDITOR
                            if (UnityEditor.EditorApplication.isPlaying)
#endif
                            onPathCompleted.Invoke();
                        }

                        if (travelMode == TravelMode.Once)
                        {
                            m_normalizedT = 1f;
                        }
                        else if (travelMode == TravelMode.Loop)
                        {
                            m_normalizedT -= 1f;
                        }
                        else
                        {
                            m_normalizedT  = 2f - m_normalizedT;
                            isGoingForward = false;
                        }
                    }
                    else
                    {
                        onPathCompletedCalledAt1 = false;
                    }
                }
                else
                {
                    m_normalizedT -= deltaTime / n_travelTime;

                    if (m_normalizedT < 0f)
                    {
                        if (!onPathCompletedCalledAt0)
                        {
                            onPathCompletedCalledAt0 = true;
#if UNITY_EDITOR
                            if (UnityEditor.EditorApplication.isPlaying)
#endif
                            onPathCompleted.Invoke();
                        }

                        if (travelMode == TravelMode.Once)
                        {
                            m_normalizedT = 0f;
                        }
                        else if (travelMode == TravelMode.Loop)
                        {
                            m_normalizedT += 1f;
                        }
                        else
                        {
                            m_normalizedT  = -m_normalizedT;
                            isGoingForward = true;
                        }
                    }
                    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;
                }
            }
        }
        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);
                    }
                }
            }
        }
Exemple #6
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;
                }
            }
        }
Exemple #7
0
        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)
                    {
                        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, extraDataLerpAsQuaternionFunction), 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, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime);
                    }
                }
            }
        }
        void Update()
        {
            cachedTransform.position = Vector3.Lerp(cachedTransform.position, spline.GetPoint(progress), movementLerpModifier * Time.deltaTime);

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

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

            if (isGoingForward)
            {
                progress += Time.deltaTime / travelTime;

                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 = false;
                    }
                }
                else
                {
                    onPathCompletedCalledAt1 = false;
                }
            }
            else
            {
                progress -= Time.deltaTime / travelTime;

                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 = true;
                    }
                }
                else
                {
                    onPathCompletedCalledAt0 = false;
                }
            }
        }