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.

58 lines
1.9 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. public class ControlsMenu : MonoBehaviour
  5. {
  6. public float spawnDistance = 0.8f;
  7. public GameObject menuSlate;
  8. public VRTK_ControllerEvents controllerEvents;
  9. public VRTK_ControllerEvents.ButtonAlias toggleButton = VRTK_ControllerEvents.ButtonAlias.ButtonTwoPress;
  10. protected bool isVisible;
  11. protected virtual void Awake()
  12. {
  13. isVisible = false;
  14. ToggleVisibility();
  15. }
  16. protected virtual void OnEnable()
  17. {
  18. if (controllerEvents != null)
  19. {
  20. controllerEvents.SubscribeToButtonAliasEvent(toggleButton, true, ToggleButtonPressed);
  21. }
  22. }
  23. protected virtual void OnDisable()
  24. {
  25. if (controllerEvents != null)
  26. {
  27. controllerEvents.UnsubscribeToButtonAliasEvent(toggleButton, true, ToggleButtonPressed);
  28. }
  29. }
  30. protected virtual void ToggleButtonPressed(object sender, ControllerInteractionEventArgs e)
  31. {
  32. isVisible = !isVisible;
  33. if (isVisible && menuSlate != null)
  34. {
  35. Transform headset = VRTK_DeviceFinder.HeadsetTransform();
  36. menuSlate.transform.position = new Vector3(headset.position.x, 0f, headset.position.z) + (headset.forward * spawnDistance);
  37. menuSlate.transform.position = new Vector3(menuSlate.transform.position.x, 0f, menuSlate.transform.position.z);
  38. Vector3 targetPosition = headset.position;
  39. targetPosition.y = menuSlate.transform.position.y;
  40. menuSlate.transform.LookAt(targetPosition);
  41. }
  42. ToggleVisibility();
  43. }
  44. protected virtual void ToggleVisibility()
  45. {
  46. if (menuSlate != null)
  47. {
  48. menuSlate.SetActive(isVisible);
  49. }
  50. }
  51. }
  52. }