RayCast2D
A 2D ray-query entity with an updated result.
RayCast2D tests a segment from its world position to the transformed TargetPosition. TargetPosition is a local endpoint, not a world coordinate. After IsColliding, read the collider, point and normal.
Results update in the physics phase. ForceRaycastUpdate requests an update now, even with Enabled = false, but does not advance the whole simulation. Configure Mask, body/area categories and exclusions. Inside hits can have zero normals; check before normalizing.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
RayCast2DConstructor
public RayCast2D();Properties7
CollideWithAreasProperty
public bool CollideWithAreas { get; set; }If true, collisions with Area2Ds will be reported.
CollideWithBodiesProperty
public bool CollideWithBodies { get; set; }If true, collisions with PhysicsBody2Ds will be reported.
CollisionMaskProperty
public uint CollisionMask { get; set; }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.
EnabledProperty
public bool Enabled { get; set; }If true, collisions will be reported.
ExcludeParentProperty
public bool ExcludeParent { get; set; }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 Entity.GetParent and RayCast2D.AddException.
HitFromInsideProperty
public bool HitFromInside { get; set; }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.
TargetPositionProperty
public Vector2 TargetPosition { get; set; }The segment endpoint local to the RayCast. DirectSpaceState.From/To instead use world coordinates; transform with ToGlobal when comparing them.
Methods28
AddExceptionvoid
public void AddException(CollisionObject2D node);Adds a collision exception so the ray does not report collisions with the specified node.
| Name | Type | Default |
|---|---|---|
node | CollisionObject2D | — |
AddExceptionRidvoid
public void AddExceptionRid(Rid rid);Adds a collision exception so the ray does not report collisions with the specified Rid.
| Name | Type | Default |
|---|---|---|
rid | Rid | — |
ClearExceptionsvoid
public void ClearExceptions();Removes all collision exceptions for this ray.
ForceRaycastUpdatevoid
public void ForceRaycastUpdate();Updates the query result immediately, including when Enabled = false. It does not advance all physics or replace correct world/access-phase selection.
GetColliderReckfellObject
public ReckfellObject GetCollider();Returns the first object that the ray intersects, or null if no object is intersecting the ray (i.e. RayCast2D.IsColliding 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.
ReckfellObjectGetColliderRidRid
public Rid 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. RayCast2D.IsColliding returns false).
RidGetColliderShapeint
public int GetColliderShape();Returns the shape ID of the first object that the ray intersects, or 0 if no object is intersecting the ray (i.e. RayCast2D.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);intGetCollisionMaskuint
public uint GetCollisionMask();uintGetCollisionMaskValuebool
public bool GetCollisionMaskValue(int layerNumber);Returns whether or not the specified layer of the RayCast2D.CollisionMask is enabled, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
boolGetCollisionNormalVector2
public Vector2 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 RayCast2D.HitFromInside is true.
Note: Check that RayCast2D.IsColliding returns true before calling this method to ensure the returned normal is valid and up-to-date.
Vector2GetCollisionPointVector2
public Vector2 GetCollisionPoint();Returns the collision point at which the ray intersects the closest object, in the global coordinate system. If RayCast2D.HitFromInside is true and the ray starts inside of a collision shape, this function will return the origin point of the ray.
Note: Check that RayCast2D.IsColliding returns true before calling this method to ensure the returned point is valid and up-to-date.
Vector2GetExcludeParentBodybool
public bool GetExcludeParentBody();boolGetTargetPositionVector2
public Vector2 GetTargetPosition();Vector2IsCollideWithAreasEnabledbool
public bool IsCollideWithAreasEnabled();boolIsCollideWithBodiesEnabledbool
public bool IsCollideWithBodiesEnabled();boolIsCollidingbool
public bool IsColliding();Returns whether any object is intersecting with the ray's vector (considering the vector length).
boolIsEnabledbool
public bool IsEnabled();boolIsHitFromInsideEnabledbool
public bool IsHitFromInsideEnabled();boolRemoveExceptionvoid
public void RemoveException(CollisionObject2D node);Removes a collision exception so the ray can report collisions with the specified node.
| Name | Type | Default |
|---|---|---|
node | CollisionObject2D | — |
RemoveExceptionRidvoid
public void RemoveExceptionRid(Rid rid);Removes a collision exception so the ray can report collisions with the specified Rid.
| Name | Type | Default |
|---|---|---|
rid | Rid | — |
SetCollideWithAreasvoid
public void SetCollideWithAreas(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetCollideWithBodiesvoid
public void SetCollideWithBodies(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetCollisionMaskvoid
public void SetCollisionMask(uint mask);| Name | Type | Default |
|---|---|---|
mask | uint | — |
SetCollisionMaskValuevoid
public void SetCollisionMaskValue(int layerNumber, bool value);Based on value, enables or disables the specified layer in the RayCast2D.CollisionMask, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
value | bool | — |
SetEnabledvoid
public void SetEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetExcludeParentBodyvoid
public void SetExcludeParentBody(bool mask);| Name | Type | Default |
|---|---|---|
mask | bool | — |
SetHitFromInsidevoid
public void SetHitFromInside(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetTargetPositionvoid
public void SetTargetPosition(Vector2 localPoint);| Name | Type | Default |
|---|---|---|
localPoint | Vector2 | — |