Raycast from camera position

I'm trying to raycast using RealityKit from the camera's current position.


func session(_ session: ARSession, didUpdate frame: ARFrame) {
  if let results = arView?.scene.raycast(origin: frame.camera.transform, direction: frame.camera.eulerAngles),
    results.count > 0 {
    if let distance = results.first?.distance,


Since there is little documentation on this, I'm struggling to translate the camera's transform into a raycast.


The following works as expected:


if let entities = arView?.entities(at: screenCenter), entities.count > 0 {
  print("entities: \(entities)")


But I cannot calculate the distance from the camera.

Replies

Ah! I got there in the end. There's a function on arview which:


Determines the position and direction of a ray through the given point in the 2D space of the view.

https://developer.apple.com/documentation/realitykit/arview/3243245-ray?changes=__3__5


Here's example:


if let ray = arView?.ray(through: screenCenter),
  let results = arView?.scene.raycast(origin: ray.origin,
                                      direction: ray.direction,
                                      length: 1.0,
                                      query: .nearest),
  results.count > 0 {

  if let grab = results.filter({ $0.distance < 0.5 }).first {
    self.remove(grab.entity)
  }
}