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.

169 lines
10 KiB

  1. // Custom Raycast|Utilities|90080
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// A Custom Raycast allows to specify custom options for a Physics.Raycast.
  7. /// </summary>
  8. /// <remarks>
  9. /// A number of other scripts can utilise a Custom Raycast to further customise the raycasts that the scripts use internally.
  10. ///
  11. /// For example, the VRTK_BodyPhysics script can be set to ignore trigger colliders when casting to see if it should teleport up or down to the nearest floor.
  12. /// </remarks>
  13. [AddComponentMenu("VRTK/Scripts/Utilities/VRTK_CustomRaycast")]
  14. public class VRTK_CustomRaycast : MonoBehaviour
  15. {
  16. [Tooltip("The layers to ignore when raycasting.")]
  17. public LayerMask layersToIgnore = Physics.IgnoreRaycastLayer;
  18. [Tooltip("Determines whether the ray will interact with trigger colliders.")]
  19. public QueryTriggerInteraction triggerInteraction = QueryTriggerInteraction.UseGlobal;
  20. /// <summary>
  21. /// The Raycast method is used to generate a raycast either from the given CustomRaycast object or a default Physics.Raycast.
  22. /// </summary>
  23. /// <param name="customCast">The optional object with customised cast parameters.</param>
  24. /// <param name="ray">The Ray to cast with.</param>
  25. /// <param name="hitData">The raycast hit data.</param>
  26. /// <param name="ignoreLayers">A layermask of layers to ignore from the raycast.</param>
  27. /// <param name="length">The maximum length of the raycast.</param>
  28. /// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param>
  29. /// <returns>Returns true if the raycast successfully collides with a valid object.</returns>
  30. public static bool Raycast(VRTK_CustomRaycast customCast, Ray ray, out RaycastHit hitData, LayerMask ignoreLayers, float length = Mathf.Infinity, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)
  31. {
  32. if (customCast != null)
  33. {
  34. return customCast.CustomRaycast(ray, out hitData, length);
  35. }
  36. else
  37. {
  38. return Physics.Raycast(ray, out hitData, length, ~ignoreLayers, affectTriggers);
  39. }
  40. }
  41. /// <summary>
  42. /// The Linecast method is used to generate a linecast either from the given CustomRaycast object or a default Physics.Linecast.
  43. /// </summary>
  44. /// <param name="customCast">The optional object with customised cast parameters.</param>
  45. /// <param name="startPosition">The world position to start the linecast from.</param>
  46. /// <param name="endPosition">The world position to end the linecast at.</param>
  47. /// <param name="hitData">The linecast hit data.</param>
  48. /// <param name="ignoreLayers">A layermask of layers to ignore from the linecast.</param>
  49. /// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param>
  50. /// <returns>Returns true if the linecast successfully collides with a valid object.</returns>
  51. public static bool Linecast(VRTK_CustomRaycast customCast, Vector3 startPosition, Vector3 endPosition, out RaycastHit hitData, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)
  52. {
  53. if (customCast != null)
  54. {
  55. return customCast.CustomLinecast(startPosition, endPosition, out hitData);
  56. }
  57. else
  58. {
  59. return Physics.Linecast(startPosition, endPosition, out hitData, ~ignoreLayers, affectTriggers);
  60. }
  61. }
  62. /// <summary>
  63. /// The CapsuleCast method is used to generate a capsulecast either from the given CustomRaycast object or a default Physics.CapsuleCast.
  64. /// </summary>
  65. /// <param name="customCast">The optional object with customised cast parameters.</param>
  66. /// <param name="point1">The center of the sphere at the start of the capsule.</param>
  67. /// <param name="point2">The center of the sphere at the end of the capsule.</param>
  68. /// <param name="radius">The radius of the capsule.</param>
  69. /// <param name="direction">The direction into which to sweep the capsule.</param>
  70. /// <param name="maxDistance">The max length of the sweep.</param>
  71. /// <param name="hitData">The capsulecast hit data.</param>
  72. /// <param name="ignoreLayers">A layermask of layers to ignore from the capsulecast.</param>
  73. /// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param>
  74. /// <returns>Returns true if the capsulecast successfully collides with a valid object.</returns>
  75. public static bool CapsuleCast(VRTK_CustomRaycast customCast, Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance, out RaycastHit hitData, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)
  76. {
  77. if (customCast != null)
  78. {
  79. return customCast.CustomCapsuleCast(point1, point2, radius, direction, maxDistance, out hitData);
  80. }
  81. else
  82. {
  83. return Physics.CapsuleCast(point1, point2, radius, direction, out hitData, maxDistance, ~ignoreLayers, affectTriggers);
  84. }
  85. }
  86. /// <summary>
  87. /// The BoxCast method is used to generate a boxcast either from the given CustomRaycast object or a default Physics.BoxCast.
  88. /// </summary>
  89. /// <param name="customCast">The optional object with customised cast parameters.</param>
  90. /// <param name="center">The center of the box.</param>
  91. /// <param name="halfExtents">Half the size of the box in each dimension.</param>
  92. /// <param name="direction">The direction in which to cast the box.</param>
  93. /// <param name="orientation">The rotation of the box.</param>
  94. /// <param name="maxDistance">The max length of the cast.</param>
  95. /// <param name="hitData">The boxcast hit data.</param>
  96. /// <param name="ignoreLayers">A layermask of layers to ignore from the boxcast.</param>
  97. /// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param>
  98. /// <returns>Returns true if the boxcast successfully collides with a valid object.</returns>
  99. public static bool BoxCast(VRTK_CustomRaycast customCast, Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation, float maxDistance, out RaycastHit hitData, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)
  100. {
  101. if (customCast != null)
  102. {
  103. return customCast.CustomBoxCast(center, halfExtents, direction, orientation, maxDistance, out hitData);
  104. }
  105. else
  106. {
  107. return Physics.BoxCast(center, halfExtents, direction, out hitData, orientation, maxDistance, ~ignoreLayers, affectTriggers);
  108. }
  109. }
  110. /// <summary>
  111. /// The CustomRaycast method is used to generate a raycast based on the options defined in the CustomRaycast object.
  112. /// </summary>
  113. /// <param name="ray">The Ray to cast with.</param>
  114. /// <param name="hitData">The raycast hit data.</param>
  115. /// <param name="length">The maximum length of the raycast.</param>
  116. /// <returns>Returns true if the raycast successfully collides with a valid object.</returns>
  117. public virtual bool CustomRaycast(Ray ray, out RaycastHit hitData, float length = Mathf.Infinity)
  118. {
  119. return Physics.Raycast(ray, out hitData, length, ~layersToIgnore, triggerInteraction);
  120. }
  121. /// <summary>
  122. /// The CustomLinecast method is used to generate a linecast based on the options defined in the CustomRaycast object.
  123. /// </summary>
  124. /// <param name="startPosition">The world position to start the linecast from.</param>
  125. /// <param name="endPosition">The world position to end the linecast at.</param>
  126. /// <param name="hitData">The linecast hit data.</param>
  127. /// <returns>Returns true if the line successfully collides with a valid object.</returns>
  128. public virtual bool CustomLinecast(Vector3 startPosition, Vector3 endPosition, out RaycastHit hitData)
  129. {
  130. return Physics.Linecast(startPosition, endPosition, out hitData, ~layersToIgnore, triggerInteraction);
  131. }
  132. /// <summary>
  133. /// The CustomCapsuleCast method is used to generate a capsulecast based on the options defined in the CustomRaycast object.
  134. /// </summary>
  135. /// <param name="point1">The center of the sphere at the start of the capsule.</param>
  136. /// <param name="point2">The center of the sphere at the end of the capsule.</param>
  137. /// <param name="radius">The radius of the capsule.</param>
  138. /// <param name="direction">The direction into which to sweep the capsule.</param>
  139. /// <param name="maxDistance">The max length of the sweep.</param>
  140. /// <param name="hitData">The capsulecast hit data.</param>
  141. /// <returns>Returns true if the capsule successfully collides with a valid object.</returns>
  142. public virtual bool CustomCapsuleCast(Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance, out RaycastHit hitData)
  143. {
  144. return Physics.CapsuleCast(point1, point2, radius, direction, out hitData, maxDistance, ~layersToIgnore, triggerInteraction);
  145. }
  146. /// <summary>
  147. /// The CustomBoxCast method is used to generate a boxcast based on the options defined in the CustomRaycast object.
  148. /// </summary>
  149. /// <param name="center">The center of the box.</param>
  150. /// <param name="halfExtents">Half the size of the box in each dimension.</param>
  151. /// <param name="direction">The direction in which to cast the box.</param>
  152. /// <param name="orientation">The rotation of the box.</param>
  153. /// <param name="maxDistance">The max length of the cast.</param>
  154. /// <param name="hitData">The boxcast hit data.</param>
  155. /// <returns>Returns true if the box successfully collides with a valid object.</returns>
  156. public virtual bool CustomBoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation, float maxDistance, out RaycastHit hitData)
  157. {
  158. return Physics.BoxCast(center, halfExtents, direction, out hitData, orientation, maxDistance, ~layersToIgnore, triggerInteraction);
  159. }
  160. }
  161. }