Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
RayCast2D
繼承: Node2D < CanvasItem < Node < Object
A ray in 2D space, used to find the first collision object it intersects.
說明
A raycast represents a ray from its origin to its target_position that finds the closest object along its path, if it intersects any.
RayCast2D can ignore some objects by adding them to an exception list, by making its detection reporting ignore Area2Ds (collide_with_areas) or PhysicsBody2Ds (collide_with_bodies), or by configuring physics layers.
RayCast2D calculates intersection every physics frame, and it holds the result until the next physics frame. For an immediate raycast, or if you want to configure a RayCast2D multiple times within the same physics frame, use force_raycast_update().
To sweep over a region of 2D space, you can approximate the region with multiple RayCast2Ds or use ShapeCast2D.
教學
屬性
|
||
|
||
|
||
|
||
|
||
|
||
|
方法
void |
add_exception(node: CollisionObject2D) |
void |
add_exception_rid(rid: RID) |
void |
|
void |
|
get_collider() const |
|
get_collider_rid() const |
|
get_collider_shape() const |
|
get_collision_mask_value(layer_number: int) const |
|
get_collision_normal() const |
|
get_collision_point() const |
|
is_colliding() const |
|
void |
|
void |
remove_exception_rid(rid: RID) |
void |
set_collision_mask_value(layer_number: int, value: bool) |
屬性說明
bool collide_with_areas = false 🔗
如果為 true,則會報告與 Area2D 的碰撞。
bool collide_with_bodies = true 🔗
如果為 true,則會報告與 PhysicsBody2D 的碰撞。
射線的碰撞遮罩。只能偵測到至少啟用了一個遮罩中碰撞層的物件。詳情請參閱文件中的《碰撞層與遮罩》。
如果為 true,將報告碰撞。
If true, this raycast will not report collisions with its parent node. This property only has an effect if the parent node is a CollisionObject2D. See also Node.get_parent() and add_exception().
bool hit_from_inside = false 🔗
如果為 true,射線會在從形狀內部開始時偵測到命中。在此情況下,碰撞法線將為 Vector2(0, 0)。不會影響凹多邊形形狀。
Vector2 target_position = Vector2(0, 50) 🔗
The ray's destination point, relative to this raycast's Node2D.position.
方法說明
void add_exception(node: CollisionObject2D) 🔗
Adds a collision exception so the ray does not report collisions with the specified node.
void add_exception_rid(rid: RID) 🔗
新增碰撞例外,這樣射線就不會報告與指定 RID 的碰撞。
void clear_exceptions() 🔗
刪除此射線的所有碰撞例外。
void force_raycast_update() 🔗
更新射線的碰撞資訊。使用該方法立即更新碰撞資訊,而不是等待下一個 _physics_process 呼叫,例如,如果射線或其父級已更改狀態。
注意:enabled 不需要為 true 即可生效。
Returns the first object that the ray intersects, or null if no object is intersecting the ray (i.e. is_colliding() returns false).
Note: This object is not guaranteed to be a CollisionObject2D. For example, if the ray intersects a TileMapLayer, the method will return a TileMapLayer instance.
RID get_collider_rid() const 🔗
返回該射線相交的第一個物件的 RID,如果沒有物件與該射線相交,則返回空 RID(即 is_colliding() 返回 false)。
int get_collider_shape() const 🔗
Returns the shape ID of the first object that the ray intersects, or 0 if no object is intersecting the ray (i.e. is_colliding() returns false).
To get the intersected shape node, for a CollisionObject2D target, use:
var target = get_collider() # A CollisionObject2D.
var shape_id = get_collider_shape() # The shape index in the collider.
var owner_id = target.shape_find_owner(shape_id) # The owner ID in the collider.
var shape = target.shape_owner_get_owner(owner_id)
var target = (CollisionObject2D)GetCollider(); // A CollisionObject2D.
var shapeId = GetColliderShape(); // The shape index in the collider.
var ownerId = target.ShapeFindOwner(shapeId); // The owner ID in the collider.
var shape = target.ShapeOwnerGetOwner(ownerId);
bool get_collision_mask_value(layer_number: int) const 🔗
返回 collision_mask 中是否啟用了指定的層,給定的 layer_number 應在 1 和 32 之間。
Vector2 get_collision_normal() const 🔗
Returns the normal of the intersecting object's shape at the collision point, or Vector2(0, 0) if the ray starts inside the shape and hit_from_inside is true.
Note: Check that is_colliding() returns true before calling this method to ensure the returned normal is valid and up-to-date.
Vector2 get_collision_point() const 🔗
Returns the collision point at which the ray intersects the closest object, in the global coordinate system. If hit_from_inside is true and the ray starts inside of a collision shape, this function will return the origin point of the ray.
Note: Check that is_colliding() returns true before calling this method to ensure the returned point is valid and up-to-date.
返回是否有任何物件與射線的向量相交(考慮向量長度)。
void remove_exception(node: CollisionObject2D) 🔗
Removes a collision exception so the ray can report collisions with the specified node.
void remove_exception_rid(rid: RID) 🔗
Removes a collision exception so the ray can report collisions with the specified RID.
void set_collision_mask_value(layer_number: int, value: bool) 🔗
根據 value,啟用或禁用 collision_mask 中指定的層,給定的 layer_number 應在 1 和 32 之間。