void Update()
    {
        //It only makes sense for us to do Catmull-Rom interpolation with at least 4 points.
        if (points.Count >= 4)
        {
            t = Mathf.Min(t + Time.deltaTime / segmentTime, 1.0f);

            int p1 = currentIndex;

            int p0 = p1 - 1;
            if (p0 < 0)
            {
                p0 = points.Count - 1;
            }

            int p2 = p1 + 1;
            if (p2 >= points.Count)
            {
                p2 = 0;
            }

            int p3 = p2 + 1;
            if (p3 >= points.Count)
            {
                p3 = 0;
            }

            transform.position = CatmullDemo_COMPLETED.Catmull(points[p0].position,
                                                               points[p1].position,
                                                               points[p2].position,
                                                               points[p3].position, t);

            if (t >= 1.0f)
            {
                StartSegment(currentIndex + 1);
            }
        }
    }
Beispiel #2
0
    void Update()
    {
        //It only makes sense for us to do Catmull-Rom interpolation with at least 4 points.
        //We also need to check that there is at least some length to the curve we made.
        if (points.Count >= 4 && totalCurveLength > 0.0f)
        {
            //Update the total distance we should have travelled based on speed.
            distanceTravelled += speed * Time.deltaTime;

            //If we exceed the total length of the curve, need to loop around.
            if (distanceTravelled >= totalCurveLength)
            {
                curSegment = 0;
                curSample  = 0;

                while (distanceTravelled > totalCurveLength)
                {
                    distanceTravelled -= totalCurveLength;
                }
            }

            //Find our segment/sample indices.
            //We can stop when the *next* sample is further than we need to be.
            bool correctIndices = curveTable[curSegment].samples[curSample + 1].accumulated > distanceTravelled;

            while (!correctIndices)
            {
                ++curSample;

                if (curSample >= samples)
                {
                    curSample = 0;
                    ++curSegment;

                    if (curSegment >= curveTable.Count)
                    {
                        curSegment = 0;
                    }
                }

                correctIndices = curveTable[curSegment].samples[curSample + 1].accumulated > distanceTravelled;
            }

            //Find the "inter-sample" t-value using inverse LERP.
            float insideT = MathUtility.InvLERP(distanceTravelled,
                                                curveTable[curSegment].samples[curSample].accumulated,
                                                curveTable[curSegment].samples[curSample + 1].accumulated);

            //Find the t-value to use for interpolation by LERPing sample t-values.
            float t = Mathf.Lerp(curveTable[curSegment].samples[curSample].t,
                                 curveTable[curSegment].samples[curSample + 1].t,
                                 insideT);

            //Figure out the indices for the points to pass to Catmull.
            int p1 = curSegment;

            int p0 = p1 - 1;
            if (p0 < 0)
            {
                p0 = points.Count - 1;
            }

            int p2 = p1 + 1;
            if (p2 >= points.Count)
            {
                p2 = 0;
            }

            int p3 = p2 + 1;
            if (p3 >= points.Count)
            {
                p3 = 0;
            }

            transform.position = CatmullDemo_COMPLETED.Catmull(points[p0].position,
                                                               points[p1].position,
                                                               points[p2].position,
                                                               points[p3].position, t);
        }
    }