RayCast2D
A ray in 2D space, used to find the first CollisionObject2D
it intersects.
class RayCast2D
A raycast represents a ray from its origin to its targetPosition
that finds the closest CollisionObject2D
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 Area2D
s (collideWithAreas
) or PhysicsBody2D
s (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 RayCast2D
s or use ShapeCast2D
.
Superclasses
class Node2D
A 2D game object, inherited by all 2D-related nodes. Has a position, rotation, scale, and Z index.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
Instance members
var collideWithAreas: Bool
If
true
, collisions withArea2D
s will be reported.var collideWithBodies: Bool
If
true
, collisions withPhysicsBody2D
s will be reported.var collisionMask: UInt32
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.
var enabled: Bool
If
true
, collisions will be reported.var excludeParent: Bool
If
true
, the parent node will be excluded from collision detection.var hitFromInside: Bool
If
true
, the ray will detect a hit when starting inside shapes. In this case the collision normal will beVector2(0, 0)
. Does not affect concave polygon shapes.var targetPosition: Vector2
The ray’s destination point, relative to the RayCast’s
position
.func addException(node: CollisionObject2D?
) Adds a collision exception so the ray does not report collisions with the specified
CollisionObject2D
node.func addExceptionRid(RID
) Adds a collision exception so the ray does not report collisions with the specified
RID
.func clearExceptions(
) Removes all collision exceptions for this ray.
func 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.func getCollider(
) -> Object? Returns the first object that the ray intersects, or
null
if no object is intersecting the ray (i.e.isColliding
returnsfalse
).func getColliderRid(
) -> RID Returns the
RID
of the first object that the ray intersects, or an emptyRID
if no object is intersecting the ray (i.e.isColliding
returnsfalse
).func getColliderShape(
) -> Int32 Returns the shape ID of the first object that the ray intersects, or
0
if no object is intersecting the ray (i.e.isColliding
returnsfalse
).func getCollisionMaskValue(layerNumber: Int32
) -> Bool Returns whether or not the specified layer of the
collisionMask
is enabled, given alayerNumber
between 1 and 32.func getCollisionNormal(
) -> Vector2 Returns the normal of the intersecting object’s shape at the collision point, or
Vector2(0, 0)
if the ray starts inside the shape andhitFromInside
istrue
.func getCollisionPoint(
) -> Vector2 Returns the collision point at which the ray intersects the closest object, in the global coordinate system. If
hitFromInside
istrue
and the ray starts inside of a collision shape, this function will return the origin point of the ray.func isColliding(
) -> Bool Returns whether any object is intersecting with the ray’s vector (considering the vector length).
func removeException(node: CollisionObject2D?
) Removes a collision exception so the ray does report collisions with the specified
CollisionObject2D
node.func removeExceptionRid(RID
) Removes a collision exception so the ray does report collisions with the specified
RID
.func setCollisionMaskValue(layerNumber: Int32, value: Bool
) Based on
value
, enables or disables the specified layer in thecollisionMask
, given alayerNumber
between 1 and 32.