private void UpdatePosition(float time) { pointsLength = spline.GetPoints().Length - 2; timeStep = 1.0f / pointsLength; currentSegment = Mathf.CeilToInt(time / timeStep); currentProgress = (time % timeStep / timeStep); currentSegmentDisplay = currentSegment; if (currentSegment < pointsLength + 1) { Vector3 posStart = spline.GetPoints()[currentSegment].position; Vector3 tanStart = spline.GetPoints()[currentSegment].tangent; Vector3 posEnd = spline.GetPoints()[currentSegment + 1].position; Vector3 tanEnd = spline.GetPoints()[currentSegment + 1].tangent; Vector3 segmentPosition = CatmullRom.CalculatePosition(posStart, posEnd, tanStart, tanEnd, currentProgress); Vector3 segmentTangent = CatmullRom.CalculateTangent(posStart, posEnd, tanStart, tanEnd, currentProgress); transform.position = Vector3.Lerp(posStart, posEnd, currentProgress); transform.rotation = Quaternion.Slerp(Quaternion.LookRotation(tanStart), Quaternion.LookRotation(tanEnd), currentProgress); } }
private void UpdatePosition(float time) { pointsLength = spline.GetPoints().Length - 2; timeStep = 1.0f / pointsLength; currentSegment = Mathf.CeilToInt(time / timeStep); currentProgress = (time % timeStep / timeStep); currentSegmentDisplay = currentSegment; if (currentSegment < pointsLength + 1) { Vector3 posStart = spline.GetPoints()[currentSegment].position; Vector3 tanStart = spline.GetPoints()[currentSegment].tangent; Vector3 posEnd = spline.GetPoints()[currentSegment + 1].position; Vector3 tanEnd = spline.GetPoints()[currentSegment + 1].tangent; Vector3 segmentPosition = CatmullRom.CalculatePosition(posStart, posEnd, tanStart, tanEnd, currentProgress); Vector3 segmentTangent = CatmullRom.CalculateTangent(posStart, posEnd, tanStart, tanEnd, currentProgress); curvePoint.position = Vector3.Lerp(posStart, posEnd, currentProgress); //cube.rotation = Quaternion.Slerp(Quaternion.LookRotation(tanStart), Quaternion.LookRotation(tanEnd), currentProgress); Quaternion q = Quaternion.Slerp(Quaternion.LookRotation(tanStart), Quaternion.LookRotation(tanEnd), currentProgress); cube.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 360f - q.eulerAngles.x)); if (q.eulerAngles.x < 180) { cube.GetComponent <Renderer>().material.color = Color.red; Debug.Log(-q.eulerAngles.x); } else { cube.GetComponent <Renderer>().material.color = Color.blue; Debug.Log(360f - q.eulerAngles.x); } } }