RayCast¶
Inherits: Spatial < Node < Object
查询与射线相交的最近物体。
描述¶
RayCast 表示从起点到终点位置的线,cast_to
。它用于检测 3D 空间,以便找到沿光线路径最近的对象。
RayCast 可以忽略某些对象,方法是通过 add_exception
将它们添加到例外列表中,或者通过使用碰撞层和掩码设置,进行适当的过滤。
RayCast 可以配置为报告与 Area(collide_with_areas) 和/或 PhysicsBody(collide_with_bodies) 的碰撞。
只有启用的光线投射才能检测空间并报告碰撞。
RayCast 计算每个物理帧的交集(参阅 Node),并将结果缓存起来,以便稍后使用,直到下一帧。如果物理帧之间(或同一帧期间)需要多次检测,请在调整光线投射后使用force_raycast_update。
教程¶
属性¶
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
方法¶
void |
add_exception ( Object node ) |
void |
add_exception_rid ( RID rid ) |
void |
clear_exceptions ( ) |
void |
|
get_collider ( ) const |
|
get_collider_shape ( ) const |
|
get_collision_mask_bit ( int bit ) const |
|
get_collision_normal ( ) const |
|
get_collision_point ( ) const |
|
is_colliding ( ) const |
|
void |
remove_exception ( Object node ) |
void |
remove_exception_rid ( RID rid ) |
void |
set_collision_mask_bit ( int bit, bool value ) |
属性说明¶
Vector3 cast_to
Default |
|
Setter |
set_cast_to(value) |
Getter |
get_cast_to() |
光线相对于光线投射的 position
的目标点,。
bool collide_with_areas
Default |
|
Setter |
set_collide_with_areas(value) |
Getter |
is_collide_with_areas_enabled() |
如果 true
,将反馈与 Area 的碰撞。
bool collide_with_bodies
Default |
|
Setter |
set_collide_with_bodies(value) |
Getter |
is_collide_with_bodies_enabled() |
如果 true
,将反馈与 PhysicsBody 的碰撞。
int collision_mask
Default |
|
Setter |
set_collision_mask(value) |
Getter |
get_collision_mask() |
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 in the documentation for more information.
Color debug_shape_custom_color
Default |
|
Setter |
set_debug_shape_custom_color(value) |
Getter |
get_debug_shape_custom_color() |
如果在 Debug 菜单中启用了可见碰撞形状 Visible Collision Shapes,则可用于在编辑器中和运行时绘制形状的自定义颜色。如果 RayCast
与某物体发生碰撞,此颜色将在运行时突出显示。
如果设置为 Color(0.0, 0.0, 0.0)
(默认),则使用 ProjectSettings.debug/shapes/collision/shape_color 中设置的颜色。
float debug_shape_thickness
Default |
|
Setter |
set_debug_shape_thickness(value) |
Getter |
get_debug_shape_thickness() |
如果设置为 1
,则将一条线用作调试形状。否则,将绘制一个截断的金字塔来表示 RayCast
。需要在 调试 菜单中启用可见碰撞形状 Visible Collision Shapes,以便调试形状在运行时可见。
bool enabled
Default |
|
Setter |
set_enabled(value) |
Getter |
is_enabled() |
如果 true
,将报告碰撞。
bool exclude_parent
Default |
|
Setter |
set_exclude_parent_body(value) |
Getter |
get_exclude_parent_body() |
如果 true
,则此 RayCast 的直接父级的碰撞将被忽略。
方法说明¶
void add_exception ( Object node )
添加碰撞例外,这样射线就不会报告与指定节点的碰撞情况。
void add_exception_rid ( RID rid )
添加碰撞例外,这样射线就不会报告与指定 RID 的碰撞。
void clear_exceptions ( )
删除此射线的所有碰撞例外。
void force_raycast_update ( )
更新射线的碰撞信息。
使用此方法立即更新碰撞信息,而不是等待下一个 _physics_process
调用,例如,如果光线或其父级已更改状态。
注意: enabled
不需要此功能。
Object get_collider ( ) const
返回射线相交的第一个对象,如果没有对象与射线相交,则返回 null
(即 is_colliding 返回 false
)。
int get_collider_shape ( ) const
返回射线相交的第一个对象的形状 ID,如果没有对象与射线相交,则返回 0
(即 is_colliding 返回 false
)。
如果通过的位索引被打开,则返回true
。
注意: 位指数范围为0-19。
Vector3 get_collision_normal ( ) const
返回碰撞点相交物体形状的法线。
Vector3 get_collision_point ( ) const
返回射线与最近的物体相交的碰撞点。
注意:这个点是在全局坐标系中。
bool is_colliding ( ) const
返回是否有任何对象与射线的向量相交(考虑向量长度)。
void remove_exception ( Object node )
移除碰撞例外,这样射线就会报告与指定节点的碰撞。
void remove_exception_rid ( RID rid )
移除碰撞例外,这样射线就会报告与指定的 RID 的碰撞。
将传递的位索引设置为传递的值
。
注意: 位索引的范围是0-19。