Exemple #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);
            }
        }
Exemple #2
0
        void DoInterpolate()
        {
            if (!mSpline.IsInitialized)
            {
                return;
            }
            GameObject go = Fsm.GetOwnerDefaultTarget(GameObject);

            if (go)
            {
                float   tf = (PositionMode == CurvyPositionMode.Relative) ? CurvyUtility.ClampTF(Position.Value, Clamping) : mSpline.DistanceToTF(Position.Value, Clamping);
                Vector3 p  = (UseCache.Value) ? mSpline.InterpolateFast(tf) : mSpline.Interpolate(tf);

                if (Space == Space.Self)
                {
                    go.transform.localPosition = p;

                    if (SetOrientation)
                    {
                        go.transform.localRotation = mSpline.GetOrientationFast(tf);
                    }
                }
                else
                {
                    go.transform.position      = mSpline.transform.TransformPoint(p);
                    go.transform.localRotation = mSpline.transform.rotation * mSpline.GetOrientationFast(tf);
                }
            }
        }
Exemple #3
0
 // Update is called once per frame
 void Update()
 {
     if (Target && Target.IsInitialized && TargetTransform)
     {
         float tf = Target.GetNearestPointTF(transform.position);
         TargetTransform.position = Target.Interpolate(tf);
     }
 }
Exemple #4
0
 void InitPosAndRot()
 {
     if (!Spline)
     {
         return;
     }
     // move the transform onto the spline
     if (Spline.Interpolate(InitialF) != mTransform.position)
     {
         mTransform.position = Spline.Interpolate(InitialF);
     }
     // Rotate the transform to match the spline's orientation?
     if (SetOrientation && mTransform.rotation != Spline.GetOrientationFast(InitialF))
     {
         mTransform.rotation = Spline.GetOrientationFast(InitialF);
     }
 }
Exemple #5
0
 void Init()
 {
     if (!Spline)
     {
         return;
     }
     mTransform.position = Spline.Interpolate(TF);
     mTransform.rotation = Spline.GetOrientationFast(TF);
     mLastCurveY         = mTransform.position.y;
 }
Exemple #6
0
    void InitPosAndRot()
    {
        if (!Spline)
        {
            return;
        }
        // Get the TF for the current distance
        float tf = Spline.DistanceToTF(InitialDistance);

        // move Transform onto the spline
        if (mTransform.position != Spline.Interpolate(tf))
        {
            mTransform.position = Spline.Interpolate(tf);
        }
        // Rotate the transform to match the spline's orientation?
        if (SetOrientation && mTransform.rotation != Spline.GetOrientationFast(tf))
        {
            mTransform.rotation = Spline.GetOrientationFast(tf);
        }
    }
Exemple #7
0
    void Set()
    {
        float tf;

        // First get the TF if needed
        if (UseWorldUnits)
        {
            if (Distance >= Spline.Length)
            {
                Distance -= Spline.Length;
            }
            else if (Distance < 0)
            {
                Distance += Spline.Length;
            }
            tf = Spline.DistanceToTF(Distance);
        }
        else
        {
            if (Distance >= 1)
            {
                Distance -= 1;
            }
            else if (Distance < 0)
            {
                Distance += 1;
            }
            tf = Distance;
        }

        // Set the position
        if (transform.position != Spline.Interpolate(tf))
        {
            transform.position = Spline.Interpolate(tf);
        }
        // Set the rotation
        if (SetOrientation && transform.rotation != Spline.GetOrientationFast(tf))
        {
            transform.rotation = Spline.GetOrientationFast(tf);
        }
    }
Exemple #8
0
        public CurvyMeshSegmentInfo(SplinePathMeshBuilder mb, float tf, float distance, Vector3 scale)
        {
            Target = mb.Spline;
            TF = tf;
            mDistance = distance;

            Vector3 p = (mb.FastInterpolation) ? Target.InterpolateFast(TF) : Target.Interpolate(TF);

            if (mb.UseWorldPosition)
                Matrix = Matrix4x4.TRS(mb.Transform.InverseTransformPoint(p), Target.GetOrientationFast(TF), scale);
            else
                Matrix = Matrix4x4.TRS(Target.Transform.InverseTransformPoint(p), Target.GetOrientationFast(TF), scale);
        }
Exemple #9
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)));
                }
            }
        }
Exemple #10
0
    public CurvyMeshSegmentInfo(SplinePathMeshBuilder mb, float tf, float distance, Vector3 scale)
    {
        Target    = mb.Spline;
        TF        = tf;
        mDistance = distance;

        Vector3 p = (mb.FastInterpolation) ? Target.InterpolateFast(TF) : Target.Interpolate(TF);

        if (mb.UseWorldPosition)
        {
            Matrix = Matrix4x4.TRS(mb.Transform.InverseTransformPoint(p), Target.GetOrientationFast(TF), scale);
        }
        else
        {
            Matrix = Matrix4x4.TRS(Target.transform.InverseTransformPoint(p), Target.GetOrientationFast(TF), scale);
        }
    }
        /*! \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);
            }
        }
Exemple #12
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;
	}
        /// <summary>
        /// Rebuilds the path.
        /// </summary>
        /// <param name="force">If true, all existing clones will be destroyed, otherwise they will be reused</param>
        /// <remarks>If you change the Source array by code, call Refresh(true)</remarks>
        public void Refresh(bool force)
        {
            if (Spline == null || !Spline.IsInitialized)
            {
                return;
            }

            // we need a spline length
            if (Spline.Length == 0)
            {
                Spline.SetDirtyAll();
            }

            checkSources();
            if (Source.Length == 0)
            {
                Clear();
                return;
            }


            // get size of clones and calculate number of clones needed
            float totaldepth;

            float[] depths = getSourceDepths(out totaldepth);

            int count = 0;

            if (!Mathf.Approximately(0, totaldepth))
            {
                switch (Mode)
                {
                case SplinePathCloneBuilderMode.CloneGroup:
                    count = Mathf.FloorToInt(Spline.Length / totaldepth) * Source.Length;
                    break;

                default:     // Individual
                    float d = Spline.Length;
                    int   i = 0;
                    while (d > 0 && count < MAXCLONES)
                    {
                        d -= depths[i++] + Gap;
                        count++;
                        if (i == Source.Length)
                        {
                            i = 0;
                        }
                    }
                    if (count != MAXCLONES)
                    {
                        count--;
                    }
                    break;
                }
            }

            // Constrain max clones
            if (count >= MAXCLONES)
            {
                Debug.LogError("SplinePathCloneBuilder: MAXCLONES reached, ensure to have proper colliders in place! If you really want to clone more than " + MAXCLONES + " objects, increase MAXCLONES in SplinePathCloneBuilder.cs (Line 15)!");
            }
            else
            {
                // Clear
                if (force)
                {
                    Clear(); // Clear all clones
                }
                else
                {
                    Clear(count); // Smart Clear only unneeded
                }
                int   idx      = 0;
                float distance = 0;
                int   current  = -1;
                int   existing = ObjectCount;

                while (++current < count)
                {
                    float tf = Spline.DistanceToTF(distance + depths[idx] / 2);

                    if (current < existing)
                    {
                        Transform T = mTransform.GetChild(current);
                        if (UseWorldPosition)
                        {
                            T.position = Spline.Interpolate(tf);
                        }

                        else
                        {
                            T.localPosition = Spline.Interpolate(tf);
                        }
                        T.rotation = Spline.GetOrientationFast(tf) * Source[idx].transform.rotation;
                    }
                    else
                    {
                        GameObject clone;
                        if (OnGetClone != null)
                        {
                            clone = OnGetClone(this, Source[idx]);
                        }
                        else
                        {
                            clone = CloneObject(Source[idx]);
                        }
                        if (clone)
                        {
                            Transform T = clone.transform;
                            T.parent   = transform;
                            clone.name = string.Format("{0:0000}", current) + clone.name;

                            if (UseWorldPosition)
                            {
                                T.position = Spline.Interpolate(tf);
                            }
                            else
                            {
                                T.localPosition = Spline.Interpolate(tf);
                            }
                            T.rotation = Spline.GetOrientationFast(tf) * Source[idx].transform.rotation;
                        }
                    }
                    distance += depths[idx] + Gap;
                    if (++idx == Source.Length)
                    {
                        idx = 0;
                    }
                }
            }
        }
    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;
    }