protected virtual void ConstructCurve(SplineInterpolator interp, SplineNode[] nInfo) { if (Speed <= 0 && nInfo.Length < 3) return; float totalLength = 0; float[] curveLengths = new float[nInfo.Length]; float[] nodeArrivalTimes = new float[nInfo.Length]; float currTime = 0; uint c = 1; bool pathEnded = false; //Cache the tag so we can reset it after we have calculated how fast the object should move. string actualTag = gameObject.tag; //Preserve the original position so that the object doesn't get deleted due to the simulation. Vector3 originalPosition = gameObject.transform.position; gameObject.tag = "Simulation"; //Swap splines just in case we use mSplineInterp elsewhere. interp.StartInterpolation( //On Path End. () => { //Debug.Log("On Path Ended"); pathEnded = true; }, //On Node Arrival. (int idxArrival, SplineNode nodeArrival) => { curveLengths[c] = mTotalSegmentSpeed; //Debug.Log("SplineController: mTotalSegmentSpeed is " + mTotalSegmentSpeed + "from node " + nInfo[idxArrival - 1].Point + " to " + nodeArrival.Point); totalLength += mTotalSegmentSpeed; mTotalSegmentSpeed = 0; ++c; }, //On Node Callback (int idxLeavingSpline, SplineNode OnNodeArrivalCallback) => { //Debug.Log("On Node callback: " + idxLeavingSpline); }, false, eWrapMode.ONCE); //Starts the Simulation. float deltaTime = 0.00005f; float currentTime = 0f; while (!pathEnded) { interp.Update(currentTime); mTotalSegmentSpeed += mSplineInterp.DistanceTraveled; currentTime += deltaTime; } //Debug.Log("SplineController: totalLength is " + totalLength); interp.Clear(); gameObject.transform.position = originalPosition; //From that, evaluate how much distance between each node makes up the curve and scale that time to be the break time. float totalTime = GetDuration(nInfo); //Debug.Log("SplineController: totalTime is " + totalTime); float averageSpeed = totalLength / totalTime; float timeToEnd = 0; float speedMultiplier = 0; for (int i = 0; i < curveLengths.Length; i++) { float hermiteLengthToEvaluate = curveLengths[i]; //Debug.Log("SplineController: hermiteLengthToEvaluate is " + hermiteLengthToEvaluate + " and the node is " + nInfo[i].Name); if (hermiteLengthToEvaluate > 0) { speedMultiplier = (hermiteLengthToEvaluate / totalLength) * (1 / Speed); timeToEnd = totalTime * speedMultiplier; //Debug.Log("SplineController: timeToEnd is " + timeToEnd); } interp.AddPoint(nInfo[i].Name, nInfo[i].Point, nInfo[i].Rot, timeToEnd + currTime, 0, new Vector2(0, 1)); currTime += timeToEnd; } gameObject.tag = actualTag; }