/// <summary> /// Rotates around a given pivot to a given angle in degrees over a specified duration /// </summary> /// <param name="transform"></param> /// <param name="pivot"></param> /// <param name="axis"></param> /// <param name="degrees"></param> /// <param name="duration"></param> /// <param name="timeScale"></param> /// <returns></returns> public static IEnumerator RotateAround(Transform transform, Vector3 pivot, Vector3 axis, float degrees, float duration, TimeScale timeScale = TimeScale.FixedDelta) { float angularSpeed = degrees / duration; float elapsed = 0f; System.Action <float> func = (float t) => { float time = timeScale.GetTime(); elapsed += time; if (elapsed >= duration) { time = time - (elapsed - duration); } float nextAngle = angularSpeed * time; transform.RotateAround(pivot, axis, nextAngle); }; if (duration <= 0f) { transform.RotateAround(pivot, axis, degrees); } else { yield return(Lerp(func, duration)); } }
/// <summary> /// Follows the specified target while the given condition is true /// </summary> /// <param name="transform"></param> /// <param name="target"></param> /// <param name="speed"></param> /// <param name="condition"></param> /// <param name="stopDistance"></param> /// <param name="timeScale"></param> /// <returns></returns> public static IEnumerator FollowWhile(Transform transform, Transform target, float speed, System.Func <bool> condition, float stopDistance = 0.0f, TimeScale timeScale = TimeScale.Delta) { while (condition.Invoke()) { FollowProcedure(transform, target, speed, stopDistance, timeScale.GetTime()); yield return(timeScale.Yield()); } }
/// <summary> /// Makes the transform follow the specified target at a specified speed and given distance until cancelled. /// Optionally, it can be forced to maintain the given distance /// </summary> /// <param name="transform"></param> /// <param name="target"></param> /// <param name="speed"></param> /// <param name="distance"></param> /// <param name="maintainDistance"></param> /// <param name="timeScale"></param> /// <returns></returns> public static IEnumerator Follow(Transform transform, Transform target, float speed, float distance = 0.0f, bool maintainDistance = false, TimeScale timeScale = TimeScale.Delta) { while (true) { FollowProcedure(transform, target, speed, distance, timeScale.GetTime(), maintainDistance); yield return(timeScale.Yield()); } }
/// <summary> /// Rotates around a given pivot until cancelled /// </summary> /// <param name="transform"></param> /// <param name="pivot"></param> /// <param name="axis"></param> /// <param name="degrees"></param> /// <returns></returns> public static IEnumerator RotateAround(Transform transform, Vector3 pivot, Vector3 axis, float degrees, System.Action onFinished = null, TimeScale timeScale = TimeScale.FixedDelta) { while (true) { float step = degrees * timeScale.GetTime(); transform.RotateAround(pivot, axis, step); yield return(timeScale.Yield()); } }
/// <summary> /// Rotates the transform to have its forward aligned towards a target until cancelled /// </summary> /// <param name="transform"></param> /// <param name="target"></param> /// <param name="speed"></param> /// <param name="timeScale"></param> /// <returns></returns> public static IEnumerator Track(Transform transform, Transform target, float speed, TimeScale timeScale = TimeScale.FixedDelta) { while (true) { Vector3 lookAtVec = target.position - transform.position; float dt = timeScale.GetTime(); transform.forward = Vector3.Lerp(transform.forward, lookAtVec, dt * speed); yield return(timeScale.Yield()); } }
/// <summary> /// The transform will follow the specified target at a specified speed for a given duration /// until the duration elapses /// </summary> /// <param name="transform"></param> /// <param name="target"></param> /// <param name="speed"></param> /// <param name="duration"></param> /// <param name="stopDistance"></param> /// <param name="timeScale"></param> /// <returns></returns> public static IEnumerator FollowUntil(Transform transform, Transform target, float speed, float duration, float stopDistance = 0.0f, TimeScale timeScale = TimeScale.Delta) { while (duration > 0f) { float dt = timeScale.GetTime(); duration -= dt; FollowProcedure(transform, target, speed, stopDistance, dt); yield return(timeScale.Yield()); } }
/// <summary> /// A routine that given a duration, returns an increasing elapsed time (until it reaches the target duration) /// </summary> /// <param name="onUpdate"></param> /// <param name="duration"></param> /// <returns></returns> public static IEnumerator Elapse(System.Action <float> onUpdate, float duration, TimeScale timeScale = TimeScale.FixedDelta) { float elapsed = 0f; while (elapsed < duration) { elapsed += timeScale.GetTime(); onUpdate(elapsed); yield return(null); } }
/// <summary> /// A routine for linearly interpolating between two values /// a and b by the interpolant t. This parameter is clamped to the range [0,1] /// </summary> /// <param name="onUpdate">The function to call each update with the t value passed to it.</param> /// <param name="duration">The duration of this interpolation.</param> /// <returns></returns> public static IEnumerator Lerp(System.Action <float> onUpdate, float duration, TimeScale timeScale = TimeScale.FixedDelta) { if (duration == 0f) { onUpdate(1f); } else { float t = 0f; while (t <= 1f) { t += timeScale.GetTime() / duration; onUpdate(t); yield return(timeScale.Yield()); } } }