BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Platform / Samples / VrBoardGame / Scripts / EyeCamera.cs

EyeCamera.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
EyeCamera.cs 2.7 KiB Download Raw
1namespace Oculus.Platform.Samples.VrBoardGame
2{
3 using UnityEngine;
4 using UnityEngine.UI;
5 using UnityEngine.EventSystems;
6
7 // This is a helper class for selecting objects that the user is looking at.
8 // It will select UI objects that have an attach 3d collision volume and helps
9 // the GameController locate GamePieces and GamePositions.
10 public class EyeCamera : MonoBehaviour
11 {
12 // the EventSystem used by the UI elements
13 [SerializeField] private EventSystem m_eventSystem = null;
14
15 // the GameController to notify
16 [SerializeField] private GameController m_gameController = null;
17
18 // a tine ball in the distance to debug where the user is looking
19 [SerializeField] private SphereCollider m_gazeTracker = null;
20
21 // the current Button, if any, being looked at
22 private Button m_currentButton;
23
24 // the current GamePiece, if any, being looked at
25 private GamePiece m_currentPiece;
26
27 // the current BoardPosition, if any, being looked at
28 private BoardPosition m_boardPosition;
29
30 void Update()
31 {
32 RaycastHit hit;
33 Button button = null;
34 GamePiece piece = null;
35 BoardPosition pos = null;
36
37 // do a forward raycast to see if we hit a selectable object
38 bool hitSomething = Physics.Raycast(transform.position, transform.forward, out hit, 50f);
39 if (hitSomething) {
40 button = hit.collider.GetComponent<Button>();
41 piece = hit.collider.GetComponent<GamePiece>();
42 pos = hit.collider.GetComponent<BoardPosition>();
43 }
44
45 if (m_currentButton != button)
46 {
47 if (m_eventSystem != null)
48 {
49 m_eventSystem.SetSelectedGameObject(null);
50 }
51 m_currentButton = button;
52 if (m_currentButton != null)
53 {
54 m_currentButton.Select();
55 }
56 }
57
58 if (m_currentPiece != piece)
59 {
60 if (m_currentPiece != null)
61 {
62 m_gameController.StoppedLookingAtPiece();
63 }
64 m_currentPiece = piece;
65 if (m_currentPiece != null)
66 {
67 m_gameController.StartedLookingAtPiece(m_currentPiece);
68 }
69 }
70
71 if (m_boardPosition != pos)
72 {
73 m_boardPosition = pos;
74 if (m_boardPosition != null)
75 {
76 m_gameController.StartedLookingAtPosition(m_boardPosition);
77 }
78 }
79
80 // clear the potential move if they gaze off the board
81 if (hit.collider == m_gazeTracker)
82 {
83 m_gameController.ClearProposedMove();
84 }
85
86 // moves the camera with the mouse - very useful for debugging in to 2D mode.
87 if (Input.GetButton("Fire2"))
88 {
89 var v = Input.GetAxis("Mouse Y");
90 var h = Input.GetAxis("Mouse X");
91 transform.rotation *= Quaternion.AngleAxis(h, Vector3.up);
92 transform.rotation *= Quaternion.AngleAxis(-v, Vector3.right);
93 Vector3 eulers = transform.eulerAngles;
94 eulers.z = 0;
95 transform.eulerAngles = eulers;
96 }
97 }
98 }
99}