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