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.

97 lines
4.4 KiB

  1. // Height Adjust Teleport|Locomotion|20020
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// Updates the `x/y/z` position of the SDK Camera Rig with an optional screen fade.
  7. /// </summary>
  8. /// <remarks>
  9. /// > The Camera Rig can be automatically teleported to the nearest floor `y` position when utilising this teleporter.
  10. ///
  11. /// **Script Usage:**
  12. /// * Place the `VRTK_HeightAdjustTeleport` script on any active scene GameObject.
  13. ///
  14. /// **Script Dependencies:**
  15. /// * An optional Destination Marker (such as a Pointer) to set the destination of the teleport location.
  16. /// </remarks>
  17. /// <example>
  18. /// `VRTK/Examples/007_CameraRig_HeightAdjustTeleport` has a collection of varying height objects that the user can either walk up and down or use the laser pointer to climb on top of them.
  19. ///
  20. /// `VRTK/Examples/010_CameraRig_TerrainTeleporting` shows how the teleportation of a user can also traverse terrain colliders.
  21. ///
  22. /// `VRTK/Examples/020_CameraRig_MeshTeleporting` shows how the teleportation of a user can also traverse mesh colliders.
  23. /// </example>
  24. [AddComponentMenu("VRTK/Scripts/Locomotion/VRTK_HeightAdjustTeleport")]
  25. public class VRTK_HeightAdjustTeleport : VRTK_BasicTeleport
  26. {
  27. [Header("Height Adjust Settings")]
  28. [Tooltip("If this is checked, then the teleported Y position will snap to the nearest available below floor. If it is unchecked, then the teleported Y position will be where ever the destination Y position is.")]
  29. public bool snapToNearestFloor = true;
  30. [Tooltip("If this is checked then the teleported Y position will also be offset by the play area parent Transform Y position (if the play area has a parent).")]
  31. public bool applyPlayareaParentOffset = false;
  32. [Tooltip("A custom raycaster to use when raycasting to find floors.")]
  33. public VRTK_CustomRaycast customRaycast;
  34. protected override void OnEnable()
  35. {
  36. base.OnEnable();
  37. adjustYForTerrain = true;
  38. AdjustForParentOffset();
  39. }
  40. protected override void OnDisable()
  41. {
  42. base.OnDisable();
  43. }
  44. protected override Vector3 GetNewPosition(Vector3 tipPosition, Transform target, bool returnOriginalPosition)
  45. {
  46. Vector3 basePosition = base.GetNewPosition(tipPosition, target, returnOriginalPosition);
  47. if (!returnOriginalPosition)
  48. {
  49. basePosition.y = GetTeleportY(target, tipPosition);
  50. }
  51. return basePosition;
  52. }
  53. protected virtual void AdjustForParentOffset()
  54. {
  55. if (snapToNearestFloor && applyPlayareaParentOffset && playArea != null && playArea.parent != null)
  56. {
  57. Ray ray = new Ray(playArea.parent.position, -playArea.up);
  58. RaycastHit rayCollidedWith;
  59. if (VRTK_CustomRaycast.Raycast(customRaycast, ray, out rayCollidedWith, Physics.IgnoreRaycastLayer, Mathf.Infinity, QueryTriggerInteraction.Ignore))
  60. {
  61. playArea.position = new Vector3(playArea.position.x, playArea.position.y + rayCollidedWith.point.y, playArea.position.z);
  62. }
  63. }
  64. }
  65. protected virtual float GetParentOffset()
  66. {
  67. return (applyPlayareaParentOffset && playArea.parent != null ? playArea.parent.transform.localPosition.y : 0f);
  68. }
  69. protected virtual float GetTeleportY(Transform target, Vector3 tipPosition)
  70. {
  71. float parentOffset = GetParentOffset();
  72. if (!snapToNearestFloor || !ValidRigObjects())
  73. {
  74. return tipPosition.y + parentOffset;
  75. }
  76. float newY = playArea.position.y;
  77. float heightOffset = 0.1f;
  78. //Check to see if the tip is on top of an object
  79. Vector3 rayStartPositionOffset = Vector3.up * heightOffset;
  80. Ray ray = new Ray(tipPosition + rayStartPositionOffset, -playArea.up);
  81. RaycastHit rayCollidedWith;
  82. if (target != null && VRTK_CustomRaycast.Raycast(customRaycast, ray, out rayCollidedWith, Physics.IgnoreRaycastLayer, Mathf.Infinity, QueryTriggerInteraction.Ignore))
  83. {
  84. newY = (tipPosition.y - rayCollidedWith.distance) + heightOffset;
  85. }
  86. return newY + parentOffset;
  87. }
  88. }
  89. }