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.

85 lines
3.4 KiB

  1. // Teleport Disable On Headset Collision|Locomotion|20040
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using System.Collections;
  6. /// <summary>
  7. /// Prevents teleportation when the HMD is colliding with valid geometry.
  8. /// </summary>
  9. /// <remarks>
  10. /// **Required Components:**
  11. /// * `VRTK_BasicTeleport` - A Teleport script to utilise for teleporting the play area.
  12. /// * `VRTK_HeadsetCollision` - A Headset Collision script for detecting when the headset has collided with valid geometry.
  13. ///
  14. /// **Script Usage:**
  15. /// * Place the `VRTK_TeleportDisableOnHeadsetCollision` script on any active scene GameObject.
  16. /// </remarks>
  17. [AddComponentMenu("VRTK/Scripts/Locomotion/VRTK_TeleportDisableOnHeadsetCollision")]
  18. public class VRTK_TeleportDisableOnHeadsetCollision : MonoBehaviour
  19. {
  20. [Header("Custom Settings")]
  21. [Tooltip("The Teleporter script to deal play area teleporting. If the script is being applied onto an object that already has a VRTK_BasicTeleport component, this parameter can be left blank as it will be auto populated by the script at runtime.")]
  22. public VRTK_BasicTeleport teleporter;
  23. [Tooltip("The VRTK Headset Collision script to use when determining headset collisions. If this is left blank then the script will need to be applied to the same GameObject.")]
  24. public VRTK_HeadsetCollision headsetCollision;
  25. protected Coroutine enableAtEndOfFrameRoutine;
  26. protected virtual void OnEnable()
  27. {
  28. teleporter = (teleporter != null ? teleporter : FindObjectOfType<VRTK_BasicTeleport>());
  29. enableAtEndOfFrameRoutine = StartCoroutine(EnableAtEndOfFrame());
  30. }
  31. protected virtual void OnDisable()
  32. {
  33. if (enableAtEndOfFrameRoutine != null)
  34. {
  35. StopCoroutine(enableAtEndOfFrameRoutine);
  36. }
  37. if (teleporter == null)
  38. {
  39. return;
  40. }
  41. if (headsetCollision != null)
  42. {
  43. headsetCollision.HeadsetCollisionDetect -= new HeadsetCollisionEventHandler(DisableTeleport);
  44. headsetCollision.HeadsetCollisionEnded -= new HeadsetCollisionEventHandler(EnableTeleport);
  45. }
  46. }
  47. protected virtual IEnumerator EnableAtEndOfFrame()
  48. {
  49. if (teleporter == null)
  50. {
  51. yield break;
  52. }
  53. yield return new WaitForEndOfFrame();
  54. headsetCollision = (headsetCollision != null ? headsetCollision : FindObjectOfType<VRTK_HeadsetCollision>());
  55. if (headsetCollision == null)
  56. {
  57. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_SCENE, "VRTK_TeleportDisableOnHeadsetCollision", "VRTK_HeadsetCollision"));
  58. }
  59. else
  60. {
  61. headsetCollision.HeadsetCollisionDetect += new HeadsetCollisionEventHandler(DisableTeleport);
  62. headsetCollision.HeadsetCollisionEnded += new HeadsetCollisionEventHandler(EnableTeleport);
  63. }
  64. }
  65. protected virtual void DisableTeleport(object sender, HeadsetCollisionEventArgs e)
  66. {
  67. teleporter.ToggleTeleportEnabled(false);
  68. }
  69. protected virtual void EnableTeleport(object sender, HeadsetCollisionEventArgs e)
  70. {
  71. teleporter.ToggleTeleportEnabled(true);
  72. }
  73. }
  74. }