SHARE
TWEET

OnFocusRotator / Anthony / Next Reality

AnthonyC Apr 25th, 2017 (edited) 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using UnityEngine;
  3. using HoloToolkit.Unity.InputModule;
  4.  
  5. public class OnFocusRotator : MonoBehaviour, IFocusable
  6. {
  7.     public Vector3 rotationAmount;
  8.     [Space(10)]
  9.     public float timeToCompleteRotation = 1;
  10.     public float timeToDefaultRotation = 1;
  11.  
  12.     private float progress;
  13.  
  14.     private Vector3 defaultRotation;
  15.  
  16.     void Start()
  17.     {
  18.         defaultRotation = transform.eulerAngles;
  19.     }
  20.  
  21.     public void OnFocusEnter()
  22.     {
  23.         if (progress == 0 || progress == 1)
  24.         {
  25.             StartCoroutine(CountProgress(timeToCompleteRotation));
  26.         }
  27.     }
  28.  
  29.     public void OnFocusExit()
  30.     {
  31.         if (progress == 0 || progress == 1)
  32.         {
  33.             StartCoroutine(CountProgress(timeToDefaultRotation * -1));
  34.         }
  35.     }
  36.  
  37.     IEnumerator CountProgress(float time)
  38.     {
  39.         Vector3 targetRotation = defaultRotation + rotationAmount;
  40.         while (progress >= 0 && progress <= 1)
  41.         {
  42.             float increment = Time.deltaTime / time;
  43.             progress += increment;
  44.             transform.eulerAngles = Vector3.Lerp(defaultRotation, targetRotation, progress);
  45.  
  46.             yield return null;
  47.         }
  48.         progress = Mathf.Clamp(progress, 0, 1);
  49.     }
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top