<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "RealityKit",
  "identifier" : "/documentation/RealityKit/Scene/raycast(from:to:query:mask:relativeTo:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "RealityKit"
    ],
    "preciseIdentifier" : "s:17RealityFoundation5SceneC7raycast4from2to5query4mask10relativeToSayAA16CollisionCastHitVGs5SIMD3VySfG_AoA0kL9QueryTypeOAA0K5GroupVAA6EntityCSgtF"
  },
  "title" : "raycast(from:to:query:mask:relativeTo:)"
}
-->

# raycast(from:to:query:mask:relativeTo:)

Performs a ray cast against all the geometry in the scene for a
ray between two end points.

```
@MainActor @preconcurrency func raycast(from startPosition: SIMD3<Float>, to endPosition: SIMD3<Float>, query: CollisionCastQueryType = .all, mask: CollisionGroup = .all, relativeTo referenceEntity: Entity? = nil) -> [CollisionCastHit]
```

## Parameters

`startPosition`

The start position of the ray relative to
`referenceEntity`.

`endPosition`

The end position of the ray relative to
`referenceEntity`.

`query`

A query type.

`mask`

A collision mask that you can use to prevent collisions with
certain objects.

`referenceEntity`

An entity that defines the frame of reference. The
method returns results relative to this entity. Set to `nil` to use the
world space origin `(0, 0, 0)`.

## Return Value

An array of collision cast hit results. Each hit indicates
where the ray, starting at `startPosition` and ending at `endPosition`,
hit a particular entity in the scene.

## Discussion

The method ignores entities that lack a [`CollisionComponent`](/documentation/RealityKit/CollisionComponent).

The [`normal`](/documentation/RealityKit/CollisionCastHit/normal) property on returned result objects
contains the surface normal at the point of intersection with the
entity’s collision shape.

The following are some details to keep in mind.

- The ray needs to fully intersect a primitive for a hit to be detected. In particular, it is not enough for the ray to precisely tangent or end at the primitive’s surface. This becomes especially important when ray casting against primitives that are far away from the ray origin.
- Due to numerical imprecision, it may be necessary to use a slightly longer ray length than your desired maximum distance in order to ensure that a full intersection occurs with your target primitive. Moreover, the length of the ray needs to be a positive finite number that is smaller than `greatestFiniteMagnitude`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)