class_raycast.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the RayCast.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_RayCast:
  6. RayCast
  7. =======
  8. **Inherits:** :ref:`Spatial<class_Spatial>` **<** :ref:`Node<class_Node>` **<** :ref:`Object<class_Object>`
  9. Query the closest object intersecting a ray.
  10. Description
  11. -----------
  12. A RayCast represents a line from its origin to its destination position, ``cast_to``. It is used to query the 3D space in order to find the closest object along the path of the ray.
  13. RayCast can ignore some objects by adding them to the exception list via ``add_exception`` or by setting proper filtering with collision layers and masks.
  14. RayCast can be configured to report collisions with :ref:`Area<class_Area>`\ s (:ref:`collide_with_areas<class_RayCast_property_collide_with_areas>`) and/or :ref:`PhysicsBody<class_PhysicsBody>`\ s (:ref:`collide_with_bodies<class_RayCast_property_collide_with_bodies>`).
  15. Only enabled raycasts will be able to query the space and report collisions.
  16. RayCast calculates intersection every physics frame (see :ref:`Node<class_Node>`), and the result is cached so it can be used later until the next frame. If multiple queries are required between physics frames (or during the same frame), use :ref:`force_raycast_update<class_RayCast_method_force_raycast_update>` after adjusting the raycast.
  17. Tutorials
  18. ---------
  19. - :doc:`../tutorials/physics/ray-casting`
  20. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/676>`__
  21. Properties
  22. ----------
  23. +-------------------------------+----------------------------------------------------------------------------------+-------------------------+
  24. | :ref:`Vector3<class_Vector3>` | :ref:`cast_to<class_RayCast_property_cast_to>` | ``Vector3( 0, -1, 0 )`` |
  25. +-------------------------------+----------------------------------------------------------------------------------+-------------------------+
  26. | :ref:`bool<class_bool>` | :ref:`collide_with_areas<class_RayCast_property_collide_with_areas>` | ``false`` |
  27. +-------------------------------+----------------------------------------------------------------------------------+-------------------------+
  28. | :ref:`bool<class_bool>` | :ref:`collide_with_bodies<class_RayCast_property_collide_with_bodies>` | ``true`` |
  29. +-------------------------------+----------------------------------------------------------------------------------+-------------------------+
  30. | :ref:`int<class_int>` | :ref:`collision_mask<class_RayCast_property_collision_mask>` | ``1`` |
  31. +-------------------------------+----------------------------------------------------------------------------------+-------------------------+
  32. | :ref:`Color<class_Color>` | :ref:`debug_shape_custom_color<class_RayCast_property_debug_shape_custom_color>` | ``Color( 0, 0, 0, 1 )`` |
  33. +-------------------------------+----------------------------------------------------------------------------------+-------------------------+
  34. | :ref:`int<class_int>` | :ref:`debug_shape_thickness<class_RayCast_property_debug_shape_thickness>` | ``2`` |
  35. +-------------------------------+----------------------------------------------------------------------------------+-------------------------+
  36. | :ref:`bool<class_bool>` | :ref:`enabled<class_RayCast_property_enabled>` | ``false`` |
  37. +-------------------------------+----------------------------------------------------------------------------------+-------------------------+
  38. | :ref:`bool<class_bool>` | :ref:`exclude_parent<class_RayCast_property_exclude_parent>` | ``true`` |
  39. +-------------------------------+----------------------------------------------------------------------------------+-------------------------+
  40. Methods
  41. -------
  42. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | void | :ref:`add_exception<class_RayCast_method_add_exception>` **(** :ref:`Object<class_Object>` node **)** |
  44. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | void | :ref:`add_exception_rid<class_RayCast_method_add_exception_rid>` **(** :ref:`RID<class_RID>` rid **)** |
  46. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | void | :ref:`clear_exceptions<class_RayCast_method_clear_exceptions>` **(** **)** |
  48. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | void | :ref:`force_raycast_update<class_RayCast_method_force_raycast_update>` **(** **)** |
  50. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`Object<class_Object>` | :ref:`get_collider<class_RayCast_method_get_collider>` **(** **)** |const| |
  52. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`int<class_int>` | :ref:`get_collider_shape<class_RayCast_method_get_collider_shape>` **(** **)** |const| |
  54. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`bool<class_bool>` | :ref:`get_collision_mask_bit<class_RayCast_method_get_collision_mask_bit>` **(** :ref:`int<class_int>` bit **)** |const| |
  56. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`Vector3<class_Vector3>` | :ref:`get_collision_normal<class_RayCast_method_get_collision_normal>` **(** **)** |const| |
  58. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`Vector3<class_Vector3>` | :ref:`get_collision_point<class_RayCast_method_get_collision_point>` **(** **)** |const| |
  60. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`bool<class_bool>` | :ref:`is_colliding<class_RayCast_method_is_colliding>` **(** **)** |const| |
  62. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | void | :ref:`remove_exception<class_RayCast_method_remove_exception>` **(** :ref:`Object<class_Object>` node **)** |
  64. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | void | :ref:`remove_exception_rid<class_RayCast_method_remove_exception_rid>` **(** :ref:`RID<class_RID>` rid **)** |
  66. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | void | :ref:`set_collision_mask_bit<class_RayCast_method_set_collision_mask_bit>` **(** :ref:`int<class_int>` bit, :ref:`bool<class_bool>` value **)** |
  68. +-------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  69. Property Descriptions
  70. ---------------------
  71. .. _class_RayCast_property_cast_to:
  72. - :ref:`Vector3<class_Vector3>` **cast_to**
  73. +-----------+-------------------------+
  74. | *Default* | ``Vector3( 0, -1, 0 )`` |
  75. +-----------+-------------------------+
  76. | *Setter* | set_cast_to(value) |
  77. +-----------+-------------------------+
  78. | *Getter* | get_cast_to() |
  79. +-----------+-------------------------+
  80. The ray's destination point, relative to the RayCast's ``position``.
  81. ----
  82. .. _class_RayCast_property_collide_with_areas:
  83. - :ref:`bool<class_bool>` **collide_with_areas**
  84. +-----------+---------------------------------+
  85. | *Default* | ``false`` |
  86. +-----------+---------------------------------+
  87. | *Setter* | set_collide_with_areas(value) |
  88. +-----------+---------------------------------+
  89. | *Getter* | is_collide_with_areas_enabled() |
  90. +-----------+---------------------------------+
  91. If ``true``, collision with :ref:`Area<class_Area>`\ s will be reported.
  92. ----
  93. .. _class_RayCast_property_collide_with_bodies:
  94. - :ref:`bool<class_bool>` **collide_with_bodies**
  95. +-----------+----------------------------------+
  96. | *Default* | ``true`` |
  97. +-----------+----------------------------------+
  98. | *Setter* | set_collide_with_bodies(value) |
  99. +-----------+----------------------------------+
  100. | *Getter* | is_collide_with_bodies_enabled() |
  101. +-----------+----------------------------------+
  102. If ``true``, collision with :ref:`PhysicsBody<class_PhysicsBody>`\ s will be reported.
  103. ----
  104. .. _class_RayCast_property_collision_mask:
  105. - :ref:`int<class_int>` **collision_mask**
  106. +-----------+---------------------------+
  107. | *Default* | ``1`` |
  108. +-----------+---------------------------+
  109. | *Setter* | set_collision_mask(value) |
  110. +-----------+---------------------------+
  111. | *Getter* | get_collision_mask() |
  112. +-----------+---------------------------+
  113. The ray's collision mask. Only objects in at least one collision layer enabled in the mask will be detected. See `Collision layers and masks <https://docs.godotengine.org/en/3.4/tutorials/physics/physics_introduction.html#collision-layers-and-masks>`__ in the documentation for more information.
  114. ----
  115. .. _class_RayCast_property_debug_shape_custom_color:
  116. - :ref:`Color<class_Color>` **debug_shape_custom_color**
  117. +-----------+-------------------------------------+
  118. | *Default* | ``Color( 0, 0, 0, 1 )`` |
  119. +-----------+-------------------------------------+
  120. | *Setter* | set_debug_shape_custom_color(value) |
  121. +-----------+-------------------------------------+
  122. | *Getter* | get_debug_shape_custom_color() |
  123. +-----------+-------------------------------------+
  124. The custom color to use to draw the shape in the editor and at run-time if **Visible Collision Shapes** is enabled in the **Debug** menu. This color will be highlighted at run-time if the ``RayCast`` is colliding with something.
  125. If set to ``Color(0.0, 0.0, 0.0)`` (by default), the color set in :ref:`ProjectSettings.debug/shapes/collision/shape_color<class_ProjectSettings_property_debug/shapes/collision/shape_color>` is used.
  126. ----
  127. .. _class_RayCast_property_debug_shape_thickness:
  128. - :ref:`int<class_int>` **debug_shape_thickness**
  129. +-----------+----------------------------------+
  130. | *Default* | ``2`` |
  131. +-----------+----------------------------------+
  132. | *Setter* | set_debug_shape_thickness(value) |
  133. +-----------+----------------------------------+
  134. | *Getter* | get_debug_shape_thickness() |
  135. +-----------+----------------------------------+
  136. If set to ``1``, a line is used as the debug shape. Otherwise, a truncated pyramid is drawn to represent the ``RayCast``. Requires **Visible Collision Shapes** to be enabled in the **Debug** menu for the debug shape to be visible at run-time.
  137. ----
  138. .. _class_RayCast_property_enabled:
  139. - :ref:`bool<class_bool>` **enabled**
  140. +-----------+--------------------+
  141. | *Default* | ``false`` |
  142. +-----------+--------------------+
  143. | *Setter* | set_enabled(value) |
  144. +-----------+--------------------+
  145. | *Getter* | is_enabled() |
  146. +-----------+--------------------+
  147. If ``true``, collisions will be reported.
  148. ----
  149. .. _class_RayCast_property_exclude_parent:
  150. - :ref:`bool<class_bool>` **exclude_parent**
  151. +-----------+--------------------------------+
  152. | *Default* | ``true`` |
  153. +-----------+--------------------------------+
  154. | *Setter* | set_exclude_parent_body(value) |
  155. +-----------+--------------------------------+
  156. | *Getter* | get_exclude_parent_body() |
  157. +-----------+--------------------------------+
  158. If ``true``, collisions will be ignored for this RayCast's immediate parent.
  159. Method Descriptions
  160. -------------------
  161. .. _class_RayCast_method_add_exception:
  162. - void **add_exception** **(** :ref:`Object<class_Object>` node **)**
  163. Adds a collision exception so the ray does not report collisions with the specified node.
  164. ----
  165. .. _class_RayCast_method_add_exception_rid:
  166. - void **add_exception_rid** **(** :ref:`RID<class_RID>` rid **)**
  167. Adds a collision exception so the ray does not report collisions with the specified :ref:`RID<class_RID>`.
  168. ----
  169. .. _class_RayCast_method_clear_exceptions:
  170. - void **clear_exceptions** **(** **)**
  171. Removes all collision exceptions for this ray.
  172. ----
  173. .. _class_RayCast_method_force_raycast_update:
  174. - void **force_raycast_update** **(** **)**
  175. Updates the collision information for the ray.
  176. Use this method to update the collision information immediately instead of waiting for the next ``_physics_process`` call, for example if the ray or its parent has changed state.
  177. **Note:** ``enabled`` is not required for this to work.
  178. ----
  179. .. _class_RayCast_method_get_collider:
  180. - :ref:`Object<class_Object>` **get_collider** **(** **)** |const|
  181. Returns the first object that the ray intersects, or ``null`` if no object is intersecting the ray (i.e. :ref:`is_colliding<class_RayCast_method_is_colliding>` returns ``false``).
  182. ----
  183. .. _class_RayCast_method_get_collider_shape:
  184. - :ref:`int<class_int>` **get_collider_shape** **(** **)** |const|
  185. Returns the shape ID of the first object that the ray intersects, or ``0`` if no object is intersecting the ray (i.e. :ref:`is_colliding<class_RayCast_method_is_colliding>` returns ``false``).
  186. ----
  187. .. _class_RayCast_method_get_collision_mask_bit:
  188. - :ref:`bool<class_bool>` **get_collision_mask_bit** **(** :ref:`int<class_int>` bit **)** |const|
  189. Returns ``true`` if the bit index passed is turned on.
  190. **Note:** Bit indices range from 0-19.
  191. ----
  192. .. _class_RayCast_method_get_collision_normal:
  193. - :ref:`Vector3<class_Vector3>` **get_collision_normal** **(** **)** |const|
  194. Returns the normal of the intersecting object's shape at the collision point.
  195. ----
  196. .. _class_RayCast_method_get_collision_point:
  197. - :ref:`Vector3<class_Vector3>` **get_collision_point** **(** **)** |const|
  198. Returns the collision point at which the ray intersects the closest object.
  199. **Note:** This point is in the **global** coordinate system.
  200. ----
  201. .. _class_RayCast_method_is_colliding:
  202. - :ref:`bool<class_bool>` **is_colliding** **(** **)** |const|
  203. Returns whether any object is intersecting with the ray's vector (considering the vector length).
  204. ----
  205. .. _class_RayCast_method_remove_exception:
  206. - void **remove_exception** **(** :ref:`Object<class_Object>` node **)**
  207. Removes a collision exception so the ray does report collisions with the specified node.
  208. ----
  209. .. _class_RayCast_method_remove_exception_rid:
  210. - void **remove_exception_rid** **(** :ref:`RID<class_RID>` rid **)**
  211. Removes a collision exception so the ray does report collisions with the specified :ref:`RID<class_RID>`.
  212. ----
  213. .. _class_RayCast_method_set_collision_mask_bit:
  214. - void **set_collision_mask_bit** **(** :ref:`int<class_int>` bit, :ref:`bool<class_bool>` value **)**
  215. Sets the bit index passed to the ``value`` passed.
  216. **Note:** Bit indexes range from 0-19.
  217. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  218. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  219. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`