Table of Contents

Class RayCast3D

Namespace
Godot
Assembly
GodotSharp.dll

A raycast represents a ray from its origin to its TargetPosition that finds the closest CollisionObject3D along its path, if it intersects any. This is useful for a lot of things, such as

RayCast3D can ignore some objects by adding them to an exception list, by making its detection reporting ignore Area3Ds (CollideWithAreas) or PhysicsBody3Ds (CollideWithBodies), or by configuring physics layers.

RayCast3D 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 RayCast3D multiple times within the same physics frame, use ForceRaycastUpdate().

To sweep over a region of 3D space, you can approximate the region with multiple RayCast3Ds or use ShapeCast3D.

public class RayCast3D : Node3D, IDisposable
Inheritance
RayCast3D
Implements
Inherited Members

Constructors

RayCast3D()

public RayCast3D()

Properties

CollideWithAreas

If true, collisions with Area3Ds will be reported.

public bool CollideWithAreas { get; set; }

Property Value

bool

CollideWithBodies

If true, collisions with PhysicsBody3Ds will be reported.

public bool CollideWithBodies { get; set; }

Property Value

bool

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

uint

DebugShapeCustomColor

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.

public Color DebugShapeCustomColor { get; set; }

Property Value

Color

DebugShapeThickness

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.

public int DebugShapeThickness { get; set; }

Property Value

int

Enabled

If true, collisions will be reported.

public bool Enabled { get; set; }

Property Value

bool

ExcludeParent

If true, collisions will be ignored for this RayCast3D's immediate parent.

public bool ExcludeParent { get; set; }

Property Value

bool

HitBackFaces

If true, the ray will hit back faces with concave polygon shapes with back face enabled or heightmap shapes.

public bool HitBackFaces { get; set; }

Property Value

bool

HitFromInside

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.

public bool HitFromInside { get; set; }

Property Value

bool

TargetPosition

The ray's destination point, relative to the RayCast's position.

public Vector3 TargetPosition { get; set; }

Property Value

Vector3

Methods

AddException(CollisionObject3D)

Adds a collision exception so the ray does not report collisions with the specified CollisionObject3D node.

public void AddException(CollisionObject3D node)

Parameters

node CollisionObject3D

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

GodotObject

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

Rid

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 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);
public int GetColliderShape()

Returns

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.

public int GetCollisionFaceIndex()

Returns

int

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

bool

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 HitFromInside is true.

public Vector3 GetCollisionNormal()

Returns

Vector3

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 Vector3 GetCollisionPoint()

Returns

Vector3

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_name

Name of the method to check for.

Returns

bool

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_name

Name of the signal to check for.

Returns

bool

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_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsColliding()

Returns whether any object is intersecting with the ray's vector (considering the vector length).

public bool IsColliding()

Returns

bool

RemoveException(CollisionObject3D)

Removes a collision exception so the ray does report collisions with the specified CollisionObject3D node.

public void RemoveException(CollisionObject3D node)

Parameters

node CollisionObject3D

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)

Parameters

layerNumber int
value bool