public override void Execute(float deltaTime) { float t = NormalizedT; BezierSpline spline = Spline; bool forward = MovingForward; for (int i = 0; i < tailObjects.Count; i++) { Transform tailObject = tailObjects[i]; if (forward) { tailObject.position = Vector3.Lerp(tailObject.position, spline.MoveAlongSpline(ref t, -tailObjectDistances[i]), movementLerpModifier * deltaTime); if (lookAt == LookAtMode.Forward) { BezierSpline.PointIndexTuple tuple = spline.GetNearestPointIndicesTo(t); tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(tuple.GetTangent(), tuple.GetNormal()), rotationLerpModifier * deltaTime); } else if (lookAt == LookAtMode.SplineExtraData) { tailObject.rotation = Quaternion.Lerp(tailObject.rotation, spline.GetExtraData(t, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime); } } else { tailObject.position = Vector3.Lerp(tailObject.position, spline.MoveAlongSpline(ref t, tailObjectDistances[i]), movementLerpModifier * deltaTime); if (lookAt == LookAtMode.Forward) { BezierSpline.PointIndexTuple tuple = spline.GetNearestPointIndicesTo(t); tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(-tuple.GetTangent(), tuple.GetNormal()), rotationLerpModifier * deltaTime); } else if (lookAt == LookAtMode.SplineExtraData) { tailObject.rotation = Quaternion.Lerp(tailObject.rotation, spline.GetExtraData(t, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime); } } } }
public override void Execute(float deltaTime) { float targetSpeed = (isGoingForward) ? speed : -speed; Vector3 targetPos = spline.MoveAlongSpline(ref m_normalizedT, targetSpeed * deltaTime); transform.position = targetPos; //transform.position = Vector3.Lerp( transform.position, targetPos, movementLerpModifier * deltaTime ); bool movingForward = MovingForward; if (lookAt == LookAtMode.Forward) { BezierSpline.PointIndexTuple tuple = spline.GetNearestPointIndicesTo(m_normalizedT); Quaternion targetRotation; if (movingForward) { targetRotation = Quaternion.LookRotation(tuple.GetTangent(), tuple.GetNormal()); } else { targetRotation = Quaternion.LookRotation(-tuple.GetTangent(), tuple.GetNormal()); } transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationLerpModifier * deltaTime); } else if (lookAt == LookAtMode.SplineExtraData) { transform.rotation = Quaternion.Lerp(transform.rotation, spline.GetExtraData(m_normalizedT, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime); } if (movingForward) { if (m_normalizedT >= 1f) { if (travelMode == TravelMode.Once) { m_normalizedT = 1f; } else if (travelMode == TravelMode.Loop) { m_normalizedT -= 1f; } else { m_normalizedT = 2f - m_normalizedT; isGoingForward = !isGoingForward; } if (!onPathCompletedCalledAt1) { onPathCompletedCalledAt1 = true; #if UNITY_EDITOR if (UnityEditor.EditorApplication.isPlaying) #endif onPathCompleted.Invoke(); } } else { onPathCompletedCalledAt1 = false; } } else { if (m_normalizedT <= 0f) { if (travelMode == TravelMode.Once) { m_normalizedT = 0f; } else if (travelMode == TravelMode.Loop) { m_normalizedT += 1f; } else { m_normalizedT = -m_normalizedT; isGoingForward = !isGoingForward; } if (!onPathCompletedCalledAt0) { onPathCompletedCalledAt0 = true; #if UNITY_EDITOR if (UnityEditor.EditorApplication.isPlaying) #endif onPathCompleted.Invoke(); } } else { onPathCompletedCalledAt0 = false; } } }