Selzier

CameraOrbit

Feb 21st, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using VRStandardAssets.Utils;
  4.  
  5. namespace VRStandardAssets.Maze
  6. {
  7.     // In the maze scene, the camera rotates around
  8.     // the maze in response to the user swiping.  This
  9.     // class handles how the camera moves in response
  10.     // to the swipe.  This script is placed on a parent
  11.     // of the camera such that the camera pivots around
  12.     // as this gameobject is rotated.
  13.     public class CameraOrbit : MonoBehaviour
  14.     {
  15.         // This enum represents the way in which the camera will rotate around the maze.
  16.         public enum OrbitStyle{
  17.             Smooth, Step, StepWithFade,
  18.         }
  19.  
  20.         [SerializeField] private OrbitStyle m_OrbitStyle;
  21.         [SerializeField] private float m_RotationIncrement = 45f;           // The amount the camera rotates in response to a swipe.
  22.         [SerializeField] private float m_RotationFadeDuration = 0.2f;       // If fading is enabled, this is the duration of the the fade.
  23.         [SerializeField] private VRCameraFade m_CameraFade;                 // Optional reference to the camera fade script, only required if fading is enabled.
  24.         [SerializeField] private VRInput m_VrInput;                         // Reference to the VRInput to subscribe to swipe events.
  25.         [SerializeField] private MazeGameController m_MazeGameController;   // Reference to the game controller so swiping will only be handled while the game is playing.
  26.         [SerializeField] private Rigidbody m_Rigidbody;                     // Reference to the camera's rigidbody.
  27.  
  28.         private Quaternion m_StartRotation;                                 // The rotation of the camera at the start of the scene, used for reseting.
  29.  
  30.         private void Awake (){
  31.             // Store the start rotation.
  32.             m_StartRotation = m_Rigidbody.rotation;
  33.         }
  34.  
  35.         private void OnEnable (){
  36.             m_VrInput.OnSwipe += HandleSwipe;
  37.         }
  38.  
  39.         private void OnDisable (){
  40.             m_VrInput.OnSwipe -= HandleSwipe;
  41.         }
  42.  
  43.         private void HandleSwipe(VRInput.SwipeDirection swipeDirection){
  44.             /*
  45.             // If the game isn't playing or the camera is fading, return and don't handle the swipe.
  46.             if (!m_MazeGameController.Playing)
  47.                 return;
  48.  
  49.             if (m_CameraFade.IsFading)
  50.                 return;
  51.  
  52.             // Otherwise start rotating the camera with either a positive or negative increment.
  53.             switch (swipeDirection)
  54.             {
  55.                 case VRInput.SwipeDirection.LEFT:
  56.                     StartCoroutine(RotateCamera(m_RotationIncrement));
  57.                     break;
  58.  
  59.                 case VRInput.SwipeDirection.RIGHT:
  60.                     StartCoroutine(RotateCamera(-m_RotationIncrement));
  61.                     break;
  62.             }
  63.             */
  64.         }
  65.  
  66.         public IEnumerator RotateCamera(float increment)
  67.         {
  68.             // Determine how the camera should rotate base on it's orbit style.
  69.             switch (m_OrbitStyle)
  70.             {
  71.                 // If the style is smooth add a torque to the camera's rigidbody.
  72.                 case OrbitStyle.Smooth:
  73.                     Debug.Log(increment);
  74.                     m_Rigidbody.AddTorque (transform.up * increment);
  75.                     break;
  76.                
  77.                 // If the style is step then rotate the camera's transform by a set amount.
  78.                 case OrbitStyle.Step:
  79.                     transform.Rotate(0, increment, 0);
  80.                     break;
  81.  
  82.                 // If the style is step with a fade, wait for the camera to fade out, then step the rotation around, the wait for the camera to fade in.
  83.                 case OrbitStyle.StepWithFade:
  84.                     yield return StartCoroutine(m_CameraFade.BeginFadeOut(m_RotationFadeDuration, false));
  85.                     transform.Rotate(0, increment, 0);
  86.                     yield return StartCoroutine(m_CameraFade.BeginFadeIn(m_RotationFadeDuration, false));
  87.                     break;
  88.             }
  89.         }
  90.  
  91.         public void Restart ()
  92.         {
  93.             // To restart, make sure the rotation is reset and the camera is not moving or rotating.
  94.             m_Rigidbody.rotation = m_StartRotation;
  95.             m_Rigidbody.angularVelocity = Vector3.zero;
  96.             m_Rigidbody.velocity = Vector3.zero;
  97.         }
  98.     }
  99. }
Add Comment
Please, Sign In to add comment