Class RayCast2D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
A raycast represents a ray from its origin to its TargetPosition that finds the closest CollisionObject2D along its path, if it intersects any. This is useful for a lot of things, such as
RayCast2D can ignore some objects by adding them to an exception list, by making its detection reporting ignore Area2Ds (CollideWithAreas) or PhysicsBody2Ds (CollideWithBodies), 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 ForceRaycastUpdate().
To sweep over a region of 2D space, you can approximate the region with multiple RayCast2Ds or use ShapeCast2D.
public class RayCast2D : Node2D, IDisposable
- Inheritance
-
RayCast2D
- Implements
- Inherited Members
Constructors
RayCast2D()
public RayCast2D()
Properties
CollideWithAreas
If true
, collisions with Area2Ds will be reported.
public bool CollideWithAreas { get; set; }
Property Value
CollideWithBodies
If true
, collisions with PhysicsBody2Ds will be reported.
public bool CollideWithBodies { get; set; }
Property Value
CollisionMask
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.
public uint CollisionMask { get; set; }
Property Value
Enabled
If true
, collisions will be reported.
public bool Enabled { get; set; }
Property Value
ExcludeParent
If true
, the parent node will be excluded from collision detection.
public bool ExcludeParent { get; set; }
Property Value
HitFromInside
If true
, the ray will detect a hit when starting inside shapes. In this case the collision normal will be Vector2(0, 0)
. Does not affect concave polygon shapes.
public bool HitFromInside { get; set; }
Property Value
TargetPosition
The ray's destination point, relative to the RayCast's position
.
public Vector2 TargetPosition { get; set; }
Property Value
Methods
AddException(CollisionObject2D)
Adds a collision exception so the ray does not report collisions with the specified CollisionObject2D node.
public void AddException(CollisionObject2D node)
Parameters
node
CollisionObject2D
AddExceptionRid(Rid)
Adds a collision exception so the ray does not report collisions with the specified Rid.
public void AddExceptionRid(Rid rid)
Parameters
rid
Rid
ClearExceptions()
Removes all collision exceptions for this ray.
public void ClearExceptions()
ForceRaycastUpdate()
Updates the collision information for the ray immediately, without waiting for the next _physics_process
call. Use this method, for example, when the ray or its parent has changed state.
Note:
Enabled does not need to be true
for this to work.
public void ForceRaycastUpdate()
GetCollider()
Returns the first object that the ray intersects, or null
if no object is intersecting the ray (i.e. IsColliding() returns false
).
public GodotObject GetCollider()
Returns
GetColliderRid()
Returns the Rid of the first object that the ray intersects, or an empty Rid if no object is intersecting the ray (i.e. IsColliding() returns false
).
public Rid GetColliderRid()
Returns
GetColliderShape()
Returns the shape ID of the first object that the ray intersects, or 0
if no object is intersecting the ray (i.e. IsColliding() returns false
).
To get the intersected shape node, for a CollisionObject2D target, use:
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);
public int GetColliderShape()
Returns
GetCollisionMaskValue(int)
Returns whether or not the specified layer of the CollisionMask is enabled, given a layerNumber
between 1 and 32.
public bool GetCollisionMaskValue(int layerNumber)
Parameters
layerNumber
int
Returns
GetCollisionNormal()
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 HitFromInside is true
.
public Vector2 GetCollisionNormal()
Returns
GetCollisionPoint()
Returns the collision point at which the ray intersects the closest object. If HitFromInside is true
and the ray starts inside of a collision shape, this function will return the origin point of the ray.
Note: This point is in the global coordinate system.
public Vector2 GetCollisionPoint()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsColliding()
Returns whether any object is intersecting with the ray's vector (considering the vector length).
public bool IsColliding()
Returns
RemoveException(CollisionObject2D)
Removes a collision exception so the ray does report collisions with the specified CollisionObject2D node.
public void RemoveException(CollisionObject2D node)
Parameters
node
CollisionObject2D
RemoveExceptionRid(Rid)
Removes a collision exception so the ray does report collisions with the specified Rid.
public void RemoveExceptionRid(Rid rid)
Parameters
rid
Rid
SetCollisionMaskValue(int, bool)
Based on value
, enables or disables the specified layer in the CollisionMask, given a layerNumber
between 1 and 32.
public void SetCollisionMaskValue(int layerNumber, bool value)