//used Time.FixedDeltaTime instead of delta time (for important physics/gameplay things) public static IEnumerator DoLerpFixedTimeRoutine(Lerpable code, float time, bool reverse = false) { if (!reverse) { float timeElapsed = 0; while (timeElapsed < time) { code(timeElapsed / time); yield return(new WaitForFixedUpdate()); timeElapsed += Time.fixedDeltaTime; } } else { float timeRemaining = time; while (timeRemaining > 0) { code(timeRemaining / time); yield return(new WaitForFixedUpdate()); timeRemaining -= Time.fixedDeltaTime; } } code(reverse ? 0 : 1); }
public static IEnumerator DoLerpUpdateTimeRoutine(Lerpable code, float time, bool reverse = false) { if (!reverse) { float timeElapsed = 0; while (timeElapsed < time) { code(timeElapsed / time); yield return(null); timeElapsed += Time.deltaTime; } } else { float timeRemaining = time; while (timeRemaining > 0) { code(timeRemaining / time); yield return(null); timeRemaining -= Time.deltaTime; } } }
private static void LerpStep(Lerpable lerpable) { // position var distCovered = (Time.time - lerpable.StartTime) * lerpable.PositionSpeed; var fracJourney = distCovered / lerpable.JourneyLength; lerpable.InnerGameObject.transform.position = Vector3.Lerp(lerpable.StartPosition, lerpable.TargetPosition, fracJourney); // angle var angleCovered = (Time.time - lerpable.StartTime) * lerpable.AngleSpeed; var angleJourney = angleCovered / lerpable.AngleLength; lerpable.InnerGameObject.transform.eulerAngles = Vector3.Lerp(lerpable.StartAngle, lerpable.TargetAngle, angleJourney); // scale var scaleCovered = (Time.time - lerpable.StartTime) * lerpable.ScaleSpeed; var scaleJourney = scaleCovered / lerpable.ScaleLength; lerpable.InnerGameObject.transform.localScale = Vector3.Lerp(lerpable.StartScale, lerpable.TargetScale, scaleJourney); if (angleJourney > 1.0f && lerpable.InnerGameObject.transform.position == lerpable.TargetPosition && lerpable.InnerGameObject.transform.localScale == lerpable.TargetScale) { lerpable.Lerping = false; } }
protected IEnumerator Lerperate(Lerpable lerpable) { while (lerpable.lerping) { lerpable.curve = curve; lerpable.Update(); yield return(new WaitForSeconds(updateRate)); } lerpable.SetToEnd(); //if (!string.IsNullOrEmpty(lerpable.message)) // RoyalHerald.TriggerEvent(lerpable.message); if (lerpables != null) { lerpables.Remove(lerpable); } }
//same, but run the lerp code independent of any timewarping public static IEnumerator DoLerpRealtimeRoutine(Lerpable code, float time, bool reverse = false) { float realStartTime = Time.realtimeSinceStartup; float realEndTime = realStartTime + time; if (!reverse) { while (Time.realtimeSinceStartup < realEndTime) { code((Time.realtimeSinceStartup - realStartTime) / time); yield return(null); } } else { while (Time.realtimeSinceStartup < realEndTime) { code((realEndTime - Time.realtimeSinceStartup) / time); yield return(null); } } }
//does a standard coroutine Lerp on a bit of code, from zero to one by default. /// <summary> /// Calls the code with the current progress over the specified time. /// </summary> /// <param name="code">The lerpable code to call.</param> /// <param name="time">The duration of the lerp.</param> /// <param name="callingScript">The <see cref="MonoBehaviour"/> to run the Coroutine on.</param> /// <param name="reverse">If <c>True</c>, calls the lerpable from 0 to 1 instead of 1 to zero.</param> /// <param name="mode">The time mode to run the Coroutine with.</param> /// <returns></returns> public static IEnumerator DoLerpRoutine(Lerpable code, float time, MonoBehaviour callingScript, bool reverse = false, Mode mode = Mode.UPDATE) { IEnumerator routine = null; switch (mode) { case Mode.UPDATE: routine = DoLerpUpdateTimeRoutine(code, time, reverse); break; case Mode.FIXEDUPDATE: routine = DoLerpFixedTimeRoutine(code, time, reverse); break; case Mode.REALTIME: routine = DoLerpRealtimeRoutine(code, time, reverse); break; } yield return(callingScript.StartCoroutine(routine)); code(reverse ? 0 : 1); }
/// <summary> /// Starts a lerp transition for a given game object /// </summary> /// <param name="gameObject">The game object to move</param> /// <param name="targetPosition">The target position of the game object, can be omitted if no transition required</param> /// <param name="targetRotation">The target rotation of the game object, can be omitted if no transition required</param> /// <param name="targetScale">The target scale of the game object, can be omitted if no transition required</param> public static void StartLerp(GameObject gameObject, Vector3 targetPosition = new Vector3(), Vector3 targetRotation = new Vector3(), Vector3 targetScale = new Vector3()) { var lerpable = new Lerpable(gameObject); lerpable.StartPosition = lerpable.InnerGameObject.transform.position; lerpable.TargetPosition = targetPosition; lerpable.StartAngle = lerpable.InnerGameObject.transform.eulerAngles; lerpable.TargetAngle = targetRotation; lerpable.StartScale = lerpable.InnerGameObject.transform.localScale; lerpable.TargetScale = targetScale; lerpable.StartTime = Time.time; lerpable.JourneyLength = Vector3.Distance(lerpable.StartPosition, lerpable.TargetPosition); lerpable.AngleLength = Vector3.Distance(lerpable.StartAngle, lerpable.TargetAngle); lerpable.ScaleLength = Vector3.Distance(lerpable.StartScale, lerpable.TargetScale); lerpable.Lerping = true; Lerpables.Add(lerpable); }
/// <summary> /// Calls the code with the current progress over the specified time. /// </summary> /// <param name="code">The lerpable code to call.</param> /// <param name="time">The duration of the lerp.</param> /// <param name="callingScript">The <see cref="MonoBehaviour"/> to run the Coroutine on.</param> /// <param name="reverse">If <c>True</c>, calls the lerpable from 0 to 1 instead of 1 to zero.</param> /// <param name="mode">The time mode to run the Coroutine with.</param> /// <returns></returns> public static Coroutine DoLerp(Lerpable code, float time, MonoBehaviour callingScript, bool reverse = false, Mode mode = Mode.UPDATE) { return RunIfActiveAndEnabled(callingScript, Routines.DoLerpRoutine(code, time, callingScript, reverse, mode)); }
public static IEnumerator DoLerpUpdateTimeRoutine(Lerpable code, float time, bool reverse = false) { if (!reverse) { float timeElapsed = 0; while (timeElapsed < time) { code(timeElapsed / time); yield return null; timeElapsed += Time.deltaTime; } } else { float timeRemaining = time; while (timeRemaining > 0) { code(timeRemaining / time); yield return null; timeRemaining -= Time.deltaTime; } } }
//does a standard coroutine Lerp on a bit of code, from zero to one by default. /// <summary> /// Calls the code with the current progress over the specified time. /// </summary> /// <param name="code">The lerpable code to call.</param> /// <param name="time">The duration of the lerp.</param> /// <param name="callingScript">The <see cref="MonoBehaviour"/> to run the Coroutine on.</param> /// <param name="reverse">If <c>True</c>, calls the lerpable from 0 to 1 instead of 1 to zero.</param> /// <param name="mode">The time mode to run the Coroutine with.</param> /// <returns></returns> public static IEnumerator DoLerpRoutine(Lerpable code, float time, MonoBehaviour callingScript, bool reverse = false, Mode mode = Mode.UPDATE) { IEnumerator routine = null; switch (mode) { case Mode.UPDATE: routine = DoLerpUpdateTimeRoutine(code, time, reverse); break; case Mode.FIXEDUPDATE: routine = DoLerpFixedTimeRoutine(code, time, reverse); break; case Mode.REALTIME: routine = DoLerpRealtimeRoutine(code, time, reverse); break; } yield return callingScript.StartCoroutine(routine); code(reverse?0:1); }
//same, but run the lerp code independent of any timewarping public static IEnumerator DoLerpRealtimeRoutine(Lerpable code, float time, bool reverse = false) { float realStartTime = Time.realtimeSinceStartup; float realEndTime = realStartTime + time; if (!reverse) { while (Time.realtimeSinceStartup < realEndTime) { code((Time.realtimeSinceStartup - realStartTime) / time); yield return null; } } else { while (Time.realtimeSinceStartup < realEndTime) { code((realEndTime - Time.realtimeSinceStartup) / time); yield return null; } } }
//used Time.FixedDeltaTime instead of delta time (for important physics/gameplay things) public static IEnumerator DoLerpFixedTimeRoutine(Lerpable code, float time, bool reverse = false) { if (!reverse) { float timeElapsed = 0; while (timeElapsed < time) { code(timeElapsed / time); yield return new WaitForFixedUpdate(); timeElapsed += Time.fixedDeltaTime; } } else { float timeRemaining = time; while (timeRemaining > 0) { code(timeRemaining / time); yield return new WaitForFixedUpdate(); timeRemaining -= Time.fixedDeltaTime; } } code(reverse ? 0 : 1); }
/// <summary> /// Calls the code with the current progress over the specified time. /// </summary> /// <param name="code">The lerpable code to call.</param> /// <param name="time">The duration of the lerp.</param> /// <param name="callingScript">The <see cref="MonoBehaviour"/> to run the Coroutine on.</param> /// <param name="reverse">If <c>True</c>, calls the lerpable from 0 to 1 instead of 1 to zero.</param> /// <param name="mode">The time mode to run the Coroutine with.</param> /// <returns></returns> public static Coroutine DoLerp(Lerpable code, float time, MonoBehaviour callingScript, bool reverse = false, Mode mode = Mode.UPDATE) { return(RunIfActiveAndEnabled(callingScript, Routines.DoLerpRoutine(code, time, callingScript, reverse, mode))); }
/// <summary> /// Gets a mobility related value /// </summary> /// <param name="l">lerpable data</param> /// <returns>The resulting value, adjusted for mobility</returns> public float GetLerpValue(Lerpable l) { return(Config.Lerpables[l].Apply(this.Mobility / 100)); }