/// <summary> /// Rotates a Transform toward a target rotation based on the given interpolation /// method in world space. /// </summary> /// <param name='xform'> /// Transform to rotate. /// </param> /// <param name='targetRot'> /// Target rot to rotate toward. /// </param> /// <param name='interpolation'> /// Interpolation mode determined by the INTERP_OPTIONS enum. /// </param> /// <param name='speed'> /// Speed to rotate. This may be used differently by each interpolation mode. /// </param> public static void InterpolateLocalRotationTo(Transform xform, Quaternion targetRot, INTERP_OPTIONS interpolation, float speed) { xform.localRotation = GetInterpolateRotationTo ( xform.localRotation, targetRot, interpolation, speed ); }
private static float lastRealtimeSinceStartup = 0; // Used for Editor work /// <summary> /// Applies the rotation options to a passed transform /// </summary> /// <param name="xform">The transform to process for</param> /// <param name="option">The UnityConstraints.OUTPUT_ROT_OPTIONS to use</param> public static void InterpolateRotationTo(Transform xform, Quaternion targetRot, INTERP_OPTIONS interpolation, float speed) { Quaternion curRot = xform.rotation; Quaternion newRot = Quaternion.identity; float deltaTime; #if UNITY_EDITOR if (Application.isPlaying) // deltaTime doesn't work in the editor { #endif deltaTime = Time.deltaTime; #if UNITY_EDITOR } else { float lastTime = UnityConstraints.lastRealtimeSinceStartup; deltaTime = Time.realtimeSinceStartup - lastTime; UnityConstraints.lastRealtimeSinceStartup = Time.realtimeSinceStartup; } #endif switch (interpolation) { case INTERP_OPTIONS.Linear: newRot = Quaternion.Lerp(curRot, targetRot, deltaTime * speed); break; case INTERP_OPTIONS.Spherical: newRot = Quaternion.Slerp(curRot, targetRot, deltaTime * speed); break; case INTERP_OPTIONS.SphericalLimited: newRot = Quaternion.RotateTowards(curRot, targetRot, speed * Time.timeScale); break; } xform.rotation = newRot; }
/// <summary> /// Gets a rotation interpolated toward a target rotation based on the given interpolation /// method. /// </summary> /// <param name='currentRot'> /// The starting rotation. /// </param> /// <param name='targetRot'> /// Target rot to rotate toward. /// </param> /// <param name='interpolation'> /// Interpolation mode determined by the INTERP_OPTIONS enum. /// </param> /// <param name='speed'> /// Speed to rotate. This may be used differently by each interpolation mode. /// </param> public static Quaternion GetInterpolateRotationTo(Quaternion currentRot, Quaternion targetRot, INTERP_OPTIONS interpolation, float speed) { var newRot = Quaternion.identity; float deltaTime; #if UNITY_EDITOR if (Application.isPlaying) // deltaTime doesn't work in the editor { #endif deltaTime = Time.deltaTime; #if UNITY_EDITOR } else { var lastTime = lastRealtimeSinceStartup; deltaTime = Time.realtimeSinceStartup - lastTime; lastRealtimeSinceStartup = Time.realtimeSinceStartup; } #endif switch (interpolation) { case INTERP_OPTIONS.Linear: newRot = Quaternion.Lerp(currentRot, targetRot, deltaTime * speed); break; case INTERP_OPTIONS.Spherical: newRot = Quaternion.Slerp(currentRot, targetRot, deltaTime * speed); break; case INTERP_OPTIONS.SphericalLimited: newRot = Quaternion.RotateTowards(currentRot, targetRot, speed * Time.timeScale); break; } return(newRot); }