Exemplo n.º 1
0
        /*! \cond PRIVATE */

        void getInterpolatedSourcePosition(CurvySplineBase spline, float tf, out Vector3 position, out Vector3 tangent, out Vector3 up)
        {
            if (spline.Length == 0)
            {
                position = spline.transform.localPosition;
                tangent  = Vector3.forward;
                up       = Vector3.up;
            }
            else
            {
                if (UseCache)
                {
                    position = spline.InterpolateFast(tf);
                    tangent  = spline.GetTangentFast(tf);
                }
                else
                {
                    position = spline.Interpolate(tf);
                    tangent  = spline.GetTangent(tf, position);
                }
                up = spline.GetOrientationUpFast(tf);
            }
            if (Space == Space.World)
            {
                position = spline.transform.TransformPoint(position);
                tangent  = spline.transform.TransformDirection(tangent);
                up       = spline.transform.TransformDirection(up);
            }
        }
Exemplo n.º 2
0
        void DoFindPoint()
        {
            if (!mSpline.IsInitialized || !SourcePoint.UseVariable)
            {
                return;
            }

            if (!StoreTF.UseVariable && !StorePosition.UseVariable && !StoreUpVector.UseVariable && !StoreRotation.UseVariable)
            {
                return;
            }

            Vector3 pos = (Space == Space.Self) ? SourcePoint.Value : mSpline.transform.InverseTransformPoint(SourcePoint.Value);

            float _tf = mSpline.GetNearestPointTF(pos);

            if (StoreTF.UseVariable)
            {
                StoreTF.Value = _tf;
            }

            if (StorePosition.UseVariable)
            {
                StorePosition.Value = (Space == Space.Self) ? mSpline.Interpolate(_tf) : mSpline.transform.TransformPoint(mSpline.Interpolate(_tf));
            }

            if (StoreTangent.UseVariable)
            {
                StoreTangent.Value = (Space == Space.Self) ? mSpline.GetTangent(_tf) : mSpline.transform.TransformDirection(mSpline.GetTangent(_tf));
            }

            if (StoreUpVector.UseVariable)
            {
                StoreUpVector.Value = (Space == Space.Self) ? mSpline.GetOrientationUpFast(_tf) : mSpline.transform.TransformDirection(mSpline.GetOrientationUpFast(_tf));
            }
            if (StoreRotation.UseVariable)
            {
                if (Space == Space.Self)
                {
                    StoreRotation.Value = (StoreUpVector.IsNone) ? mSpline.GetOrientationFast(_tf) : Quaternion.LookRotation(mSpline.GetTangent(_tf), StoreUpVector.Value);
                }
                else
                {
                    StoreRotation.Value = Quaternion.LookRotation(mSpline.transform.TransformDirection(mSpline.GetTangent(_tf)), mSpline.transform.TransformDirection(mSpline.GetOrientationUpFast(_tf)));
                }
            }
        }
Exemplo n.º 3
0
        /*! \cond PRIVATE */

        void getInterpolatedSourcePosition(CurvySplineBase spline,float tf, out Vector3 position, out Vector3 tangent, out Vector3 up)
        {
            if (UseCache)
            {
                position = spline.InterpolateFast(tf);
                tangent = spline.GetTangentFast(tf);
            }
            else
            {
                position = spline.Interpolate(tf);
                tangent = spline.GetTangent(tf, position);
            }
            up = spline.GetOrientationUpFast(tf);
            if (Space == Space.World)
            {
                position = spline.transform.TransformPoint(position);
                tangent = spline.transform.TransformDirection(tangent);
                up = spline.transform.TransformDirection(up);
            }
        }
Exemplo n.º 4
0
        void DoInterpolate()
        {
            if (!mSpline.IsInitialized)
            {
                return;
            }
            System.Type metaType = System.Type.GetType(MetaDataType.Value);
            bool        calc     = !Input.IsNone;

            if (calc)
            {
                float f = (UseWorldUnits.Value) ? mSpline.DistanceToTF(Input.Value) : Input.Value;

                if (StorePosition.UseVariable)
                {
                    StorePosition.Value = (UseCache.Value) ? mSpline.InterpolateFast(f) : mSpline.Interpolate(f);
                }

                if (StoreTangent.UseVariable)
                {
                    StoreTangent.Value = mSpline.GetTangent(f);
                }

                if (StoreUpVector.UseVariable)
                {
                    StoreUpVector.Value = mSpline.GetOrientationUpFast(f);
                }

                if (StoreRotation.UseVariable)
                {
                    StoreRotation.Value = (StoreUpVector.IsNone) ? mSpline.GetOrientationFast(f) : Quaternion.LookRotation(mSpline.GetTangent(f), StoreUpVector.Value);
                }

                if (StoreScale.UseVariable)
                {
                    StoreScale.Value = mSpline.InterpolateScale(f);
                }

                if (StoreTF.UseVariable)
                {
                    StoreTF.Value = f;
                }

                if (StoreDistance.UseVariable)
                {
                    StoreDistance.Value = (UseWorldUnits.Value) ? Input.Value : mSpline.TFToDistance(f);
                }
                if (metaType != null)
                {
                    if (StoreMetadata.UseVariable)
                    {
                        StoreMetadata.Value = mSpline.GetMetadata(metaType, f);
                    }
                    if (StoreInterpolatedMetadata.useVariable)
                    {
                        StoreInterpolatedMetadata.SetValue(mSpline.InterpolateMetadata(metaType, f));
                    }
                }


                CurvySplineSegment seg = null;
                float segF             = 0;
                if (StoreSegment.UseVariable)
                {
                    seg = getSegment(f, out segF);
                    StoreSegment.Value = seg.gameObject;
                }

                if (StoreSegmentF.UseVariable)
                {
                    if (!seg)
                    {
                        seg = getSegment(f, out segF);
                    }
                    StoreSegmentF.Value = segF;
                }

                if (StoreSegmentDistance.UseVariable)
                {
                    if (!seg)
                    {
                        seg = getSegment(f, out segF);
                    }
                    StoreSegmentDistance.Value = seg.LocalFToDistance(segF);
                }
            }
            // General
            if (StoreLength.UseVariable)
            {
                StoreLength.Value = mSpline.Length;
            }

            if (StoreCount.UseVariable)
            {
                StoreCount.Value = (mSpline is CurvySplineGroup) ? ((CurvySplineGroup)mSpline).Count : ((CurvySpline)mSpline).Count;
            }
        }
    void Update()
    {
        if (!Spline || !Spline.IsInitialized)
        {
            return;
        }

        // *** Place Player in editor ***
        if (!Application.isPlaying)
        {
            if (Spline.Interpolate(TF) != mTransform.position)
            {
                mTransform.position = Spline.Interpolate(TF);
            }
            return;
        }

        int dir = 1;

        // advance on lane. We use PingPong clamping to detect when we reach the end of a spline (in that case dir changes!)
        Vector3 newPosOnSpline = Spline.MoveBy(ref TF, ref dir, Speed * Time.deltaTime, CurvyClamping.PingPong);
        Vector3 newTangent     = Spline.GetTangent(TF);

        // Advance by spline curvation delta. We don't use newPosOnSpline directly because we don't want a snap-effect when in air or switching lanes
        mTransform.position += newPosOnSpline - mLastPosOnSpline;

        // *** Switch lanes? ***
        if (Input.GetButtonDown("Horizontal"))
        {
            if (TrySwitchLane(Input.GetAxis("Horizontal") < 0 ? Vector3.left : Vector3.right))
            {
                newPosOnSpline = Spline.Interpolate(TF);
            }
        }
        // *** Jump? ***
        if (!mInAir && !Jumping && Input.GetKey(KeyCode.Space))
        {
            StartCoroutine(Jump());
        }

        // Set orientation
        mTransform.forward = newTangent;

        // Oops! We've reached end of spline. Check if we can fall down onto another spline...
        if (dir != 1)
        {
            CurvySplineBase newSpline;
            float           newTF;
            if (RaycastForFollowUpSpline(Vector3.down, out newSpline, out newTF))
            {
                // we found a new spline underneath us. Let's use it from now on
                Spline         = newSpline;
                TF             = newTF;
                newPosOnSpline = Spline.Interpolate(TF);
                mInAir         = (newPosOnSpline - mTransform.position).sqrMagnitude > 0.001f;
            }
            else
            {
                StartCoroutine(Die()); // No Spline found. Time to die...
            }
        }


        // When in air or switching lanes, our position isn't where it should be. So we drag the player toward the lane position
        if (!Jumping)
        {
            Vector3 offset = mTransform.position - newPosOnSpline;
            if (offset.sqrMagnitude > 0.001f)
            {
                if (mInAir)
                {
                    mTransform.position -= offset.normalized * Gravity * Time.deltaTime;
                }
                else
                {
                    mTransform.position -= offset.normalized * SwitchSpeed * Time.deltaTime;
                }
            }
            else
            {
                mInAir = false;
            }
        }
        else   // Perform a jump
        {
            mTransform.Translate(0, mJumpDelta * Time.deltaTime, 0, Space.Self);
        }



        mLastPosOnSpline = newPosOnSpline;
    }