Guest User

TeleportTransitionBlink_Fixed

a guest
Nov 14th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. /************************************************************************************
  2.  
  3. See SampleFramework license.txt for license terms.  Unless required by applicable law
  4. or agreed to in writing, the sample code is provided “AS IS” WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.  See the license for specific
  6. language governing permissions and limitations under the license.
  7.  
  8. ************************************************************************************/
  9.  
  10. using System;
  11. using UnityEngine;
  12. using System.Collections;
  13.  
  14. /// <summary>
  15. /// This transition will cause the screen to quickly fade to black, perform the repositioning, and then fade
  16. /// the view back to normal.
  17. /// </summary>
  18. public class TeleportTransitionBlink : TeleportTransition
  19. {
  20.     public OVRScreenFade fader;
  21.     /// <summary>
  22.     /// How long the transition takes. Usually this is greater than Teleport Delay.
  23.     /// </summary>
  24.     [Tooltip("How long the transition takes. Usually this is greater than Teleport Delay.")]
  25.     [Range(0.01f, 2.0f)]
  26.     public float TransitionDuration = 0.5f;
  27.  
  28.     /// <summary>
  29.     /// At what percentage of the elapsed transition time does the teleport occur?
  30.     /// </summary>
  31.     [Tooltip("At what percentage of the elapsed transition time does the teleport occur?")]
  32.     [Range(0.0f,1.0f)]
  33.     public float TeleportDelay = 0.5f;
  34.  
  35.     /// <summary>
  36.     /// Fade to black over the duration of the transition.
  37.     /// </summary>
  38.     [Tooltip("Fade to black over the duration of the transition")]
  39.     public AnimationCurve FadeLevels = new AnimationCurve(new Keyframe[3] { new Keyframe(0,0), new Keyframe(0.5f, 1.0f), new Keyframe(1.0f, 0.0f) });
  40.    
  41.     /// <summary>
  42.     /// When the teleport state is entered, start a coroutine that will handle the
  43.     /// actual transition effect.
  44.     /// </summary>
  45.     protected override void LocomotionTeleportOnEnterStateTeleporting()
  46.     {
  47.         StartCoroutine(BlinkCoroutine());
  48.     }
  49.  
  50.     /// <summary>
  51.     /// This coroutine will fade out the view, perform the teleport, and then fade the view
  52.     /// back in.
  53.     /// </summary>
  54.     /// <returns></returns>
  55.     protected IEnumerator BlinkCoroutine()
  56.     {
  57.         LocomotionTeleport.IsTransitioning = true;
  58.         float elapsedTime = 0;
  59.         var teleportTime = TransitionDuration * TeleportDelay;
  60.         var teleported = false;
  61.         while (elapsedTime < TransitionDuration)
  62.         {
  63.             yield return null;
  64.             elapsedTime += Time.deltaTime;
  65.             if (!teleported && elapsedTime >= teleportTime)
  66.             {
  67.                 teleported = true;
  68.                 LocomotionTeleport.DoTeleport();
  69.             }
  70.             float fadeLevel = FadeLevels.Evaluate(elapsedTime / TransitionDuration);
  71.             fader.SetFadeLevel(fadeLevel);
  72.         }
  73.  
  74.         fader.SetFadeLevel(0);
  75.  
  76.         LocomotionTeleport.IsTransitioning = false;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment