RayCast2D¶
Inherits: Node2D < CanvasItem < Node < Object
查询与射线相交的最近物体。
描述¶
RayCast 表示一条从原点到目标位置 cast_to
的直线。它被用来查询2D空间,以便沿着射线的路径找到最近的物体。
RayCast2D 可以忽略一些物体,通过 add_exception
将它们添加到异常列表中,通过设置碰撞层进行适当的过滤,或者通过类型掩码过滤物体类型。
RayCast2D 可以被配置为报告 Area2D(collide_with_areas)和 PhysicsBody2D(collide_with_bodies)之间或彼此间的碰撞。
只有被激活的 Raycast 才能够查询空间并报告碰撞情况。
RayCast2D 在每一个物理帧中计算交集(见 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 ) |
属性说明¶
Vector2 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
,将报告与Area2D的碰撞。
bool collide_with_bodies
Default |
|
Setter |
set_collide_with_bodies(value) |
Getter |
is_collide_with_bodies_enabled() |
如果true
,会报告与PhysicsBody2D的碰撞。
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.
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
,父节点将被排除在碰撞检测之外。
方法说明¶
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
)。
返回碰撞遮罩上的单个的个体。
Vector2 get_collision_normal ( ) const
返回碰撞点相交物体形状的法线。
Vector2 get_collision_point ( ) const
返回射线与最近的物体相交的碰撞点。
注意:这个点是在全局坐标系中。
bool is_colliding ( ) const
返回是否有任何对象与射线的向量相交(考虑向量长度)。
void remove_exception ( Object node )
移除碰撞例外,这样射线就会报告与指定节点的碰撞。
void remove_exception_rid ( RID rid )
移除碰撞例外,这样射线就会报告与指定的 RID 的碰撞。
设置或清除碰撞掩码上的单个位。这使得选择扫描区域更容易。