Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.7 KiB

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