FallbackCameraController.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 up = 0.0f;
  38. if (Input.GetKey(KeyCode.E))
  39. {
  40. up += 1.0f;
  41. }
  42. if (Input.GetKey(KeyCode.Q))
  43. {
  44. up -= 1.0f;
  45. }
  46. float right = 0.0f;
  47. if ( Input.GetKey( KeyCode.D ) || Input.GetKey( KeyCode.RightArrow ) )
  48. {
  49. right += 1.0f;
  50. }
  51. if ( Input.GetKey( KeyCode.A ) || Input.GetKey( KeyCode.LeftArrow ) )
  52. {
  53. right -= 1.0f;
  54. }
  55. float currentSpeed = speed;
  56. if ( Input.GetKey( KeyCode.LeftShift ) || Input.GetKey( KeyCode.RightShift ) )
  57. {
  58. currentSpeed = shiftSpeed;
  59. }
  60. float realTimeNow = Time.realtimeSinceStartup;
  61. float deltaRealTime = realTimeNow - realTime;
  62. realTime = realTimeNow;
  63. Vector3 delta = new Vector3( right, up, forward ) * currentSpeed * deltaRealTime;
  64. transform.position += transform.TransformDirection( delta );
  65. Vector3 mousePosition = Input.mousePosition;
  66. if ( Input.GetMouseButtonDown( 1 ) /* right mouse */)
  67. {
  68. startMousePosition = mousePosition;
  69. startEulerAngles = transform.localEulerAngles;
  70. }
  71. if ( Input.GetMouseButton( 1 ) /* right mouse */)
  72. {
  73. Vector3 offset = mousePosition - startMousePosition;
  74. transform.localEulerAngles = startEulerAngles + new Vector3( -offset.y * 360.0f / Screen.height, offset.x * 360.0f / Screen.width, 0.0f );
  75. }
  76. }
  77. //-------------------------------------------------
  78. void OnGUI()
  79. {
  80. if ( showInstructions )
  81. {
  82. GUI.Label( new Rect( 10.0f, 10.0f, 600.0f, 400.0f ),
  83. "WASD EQ/Arrow Keys to translate the camera\n" +
  84. "Right mouse click to rotate the camera\n" +
  85. "Left mouse click for standard interactions.\n" );
  86. }
  87. }
  88. }
  89. }