RayCast3D
A 3D ray-query entity with an updated result.
RayCast3D 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
RayCast3DConstructor
public RayCast3D();Properties10
CollideWithAreasProperty
public bool CollideWithAreas { get; set; }If true, collisions with Area3Ds will be reported.
CollideWithBodiesProperty
public bool CollideWithBodies { get; set; }If true, collisions with PhysicsBody3Ds 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.
DebugShapeCustomColorProperty
public Color DebugShapeCustomColor { get; set; }The custom color to use to draw the shape in the editor and at run-time if Visible Collision Shapes is enabled in the Debug menu. This color will be highlighted at run-time if the RayCast3D is colliding with something.
If set to Color(0.0, 0.0, 0.0) (by default), the color set in ProjectSettings.debug/shapes/collision/shape_color is used.
DebugShapeThicknessProperty
public int DebugShapeThickness { get; set; }If set to 1, a line is used as the debug shape. Otherwise, a truncated pyramid is drawn to represent the RayCast3D. Requires Visible Collision Shapes to be enabled in the Debug menu for the debug shape to be visible at run-time.
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 CollisionObject3D. See also Entity.GetParent and RayCast3D.AddException.
HitBackFacesProperty
public bool HitBackFaces { get; set; }If true, the ray will hit back faces with concave polygon shapes with back face enabled or heightmap shapes.
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 Vector3(0, 0, 0). Does not affect shapes with no volume like concave polygon or heightmap.
TargetPositionProperty
public Vector3 TargetPosition { get; set; }The segment endpoint local to the RayCast. DirectSpaceState.From/To instead use world coordinates; transform with ToGlobal when comparing them.
Methods35
AddExceptionvoid
public void AddException(CollisionObject3D node);Adds a collision exception so the ray does not report collisions with the specified node.
| Name | Type | Default |
|---|---|---|
node | CollisionObject3D | — |
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. RayCast3D.IsColliding returns false).
Note: This object is not guaranteed to be a CollisionObject3D. For example, if the ray intersects a CsgShape3D or a GridMap, the method will return a CsgShape3D or GridMap 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. RayCast3D.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. RayCast3D.IsColliding returns false).
To get the intersected shape node, for a CollisionObject3D target, use:
var target = (CollisionObject3D)GetCollider(); // A CollisionObject3D.
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);intGetCollisionFaceIndexint
public int GetCollisionFaceIndex();Returns the collision object's face index at the collision point, or -1 if the shape intersecting the ray is not a ConcavePolygonShape3D.
intGetCollisionMaskuint
public uint GetCollisionMask();uintGetCollisionMaskValuebool
public bool GetCollisionMaskValue(int layerNumber);Returns whether or not the specified layer of the RayCast3D.CollisionMask is enabled, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
boolGetCollisionNormalVector3
public Vector3 GetCollisionNormal();Returns the normal of the intersecting object's shape at the collision point, or Vector3(0, 0, 0) if the ray starts inside the shape and RayCast3D.HitFromInside is true.
Note: Check that RayCast3D.IsColliding returns true before calling this method to ensure the returned normal is valid and up-to-date.
Vector3GetCollisionPointVector3
public Vector3 GetCollisionPoint();Returns the collision point at which the ray intersects the closest object, in the global coordinate system. If RayCast3D.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 RayCast3D.IsColliding returns true before calling this method to ensure the returned point is valid and up-to-date.
Vector3GetDebugShapeCustomColorColor
public Color GetDebugShapeCustomColor();ColorGetDebugShapeThicknessint
public int GetDebugShapeThickness();intGetExcludeParentBodybool
public bool GetExcludeParentBody();boolGetTargetPositionVector3
public Vector3 GetTargetPosition();Vector3IsCollideWithAreasEnabledbool
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();boolIsHitBackFacesEnabledbool
public bool IsHitBackFacesEnabled();boolIsHitFromInsideEnabledbool
public bool IsHitFromInsideEnabled();boolRemoveExceptionvoid
public void RemoveException(CollisionObject3D node);Removes a collision exception so the ray can report collisions with the specified node.
| Name | Type | Default |
|---|---|---|
node | CollisionObject3D | — |
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 RayCast3D.CollisionMask, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
value | bool | — |
SetDebugShapeCustomColorvoid
public void SetDebugShapeCustomColor(Color debugShapeCustomColor);| Name | Type | Default |
|---|---|---|
debugShapeCustomColor | Color | — |
SetDebugShapeThicknessvoid
public void SetDebugShapeThickness(int debugShapeThickness);| Name | Type | Default |
|---|---|---|
debugShapeThickness | int | — |
SetEnabledvoid
public void SetEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetExcludeParentBodyvoid
public void SetExcludeParentBody(bool mask);| Name | Type | Default |
|---|---|---|
mask | bool | — |
SetHitBackFacesvoid
public void SetHitBackFaces(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetHitFromInsidevoid
public void SetHitFromInside(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetTargetPositionvoid
public void SetTargetPosition(Vector3 localPoint);| Name | Type | Default |
|---|---|---|
localPoint | Vector3 | — |