FallbackCameraController.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Controls for the non-VR debug camera
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. [RequireComponent( typeof( Camera ) )]
  12. public class FallbackCameraController : MonoBehaviour
  13. {
  14. public float speed = 4.0f;
  15. public float shiftSpeed = 16.0f;
  16. public bool showInstructions = true;
  17. private Vector3 startEulerAngles;
  18. private Vector3 startMousePosition;
  19. private float realTime;
  20. //-------------------------------------------------
  21. void OnEnable()
  22. {
  23. realTime = Time.realtimeSinceStartup;
  24. }
  25. //-------------------------------------------------
  26. void Update()
  27. {
  28. float forward = 0.0f;
  29. if ( Input.GetKey( KeyCode.W ) || Input.GetKey( KeyCode.UpArrow ) )
  30. {
  31. forward += 1.0f;
  32. }
  33. if ( Input.GetKey( KeyCode.S ) || Input.GetKey( KeyCode.DownArrow ) )
  34. {
  35. forward -= 1.0f;
  36. }
  37. float right = 0.0f;
  38. if ( Input.GetKey( KeyCode.D ) || Input.GetKey( KeyCode.RightArrow ) )
  39. {
  40. right += 1.0f;
  41. }
  42. if ( Input.GetKey( KeyCode.A ) || Input.GetKey( KeyCode.LeftArrow ) )
  43. {
  44. right -= 1.0f;
  45. }
  46. float currentSpeed = speed;
  47. if ( Input.GetKey( KeyCode.LeftShift ) || Input.GetKey( KeyCode.RightShift ) )
  48. {
  49. currentSpeed = shiftSpeed;
  50. }
  51. float realTimeNow = Time.realtimeSinceStartup;
  52. float deltaRealTime = realTimeNow - realTime;
  53. realTime = realTimeNow;
  54. Vector3 delta = new Vector3( right, 0.0f, forward ) * currentSpeed * deltaRealTime;
  55. transform.position += transform.TransformDirection( delta );
  56. Vector3 mousePosition = Input.mousePosition;
  57. if ( Input.GetMouseButtonDown( 1 ) /* right mouse */)
  58. {
  59. startMousePosition = mousePosition;
  60. startEulerAngles = transform.localEulerAngles;
  61. }
  62. if ( Input.GetMouseButton( 1 ) /* right mouse */)
  63. {
  64. Vector3 offset = mousePosition - startMousePosition;
  65. transform.localEulerAngles = startEulerAngles + new Vector3( -offset.y * 360.0f / Screen.height, offset.x * 360.0f / Screen.width, 0.0f );
  66. }
  67. }
  68. //-------------------------------------------------
  69. void OnGUI()
  70. {
  71. if ( showInstructions )
  72. {
  73. GUI.Label( new Rect( 10.0f, 10.0f, 600.0f, 400.0f ),
  74. "WASD/Arrow Keys to translate the camera\n" +
  75. "Right mouse click to rotate the camera\n" +
  76. "Left mouse click for standard interactions.\n" );
  77. }
  78. }
  79. }
  80. }