Repeats a ray-cast query over time to notify you of updated surfaces in the physical environment.
How to use this func in ARKit?
func trackedRaycast(_ query: ARRaycastQuery,
updateHandler: @escaping ([ARRaycastResult]) -> Void) -> ARTrackedRaycast?
Apple Developer documentation says the following about trackedRaycast()
:
trackedRaycast(_:updateHandler:)
instance method repeats a ray-cast query over time to notify you of updated surfaces in the physical environment. A tracked ray cast wraps a ray-cast query that theARSession
calls repeatedly, each time invoking your update handler to provide you with new results. When you're ready to stop a tracked ray cast, callstopTracking()
.
So, here's a code snippet that you can use in your project:
import RealityKit
import ARKit
@IBOutlet var arView: ARView!
let scene = try! Experience.loadScene()
let raycastQuery: ARRaycastQuery = .init(origin: [0,0,0],
direction: [7,7,7],
allowing: .estimatedPlane,
alignment: .horizontal)
let repeatRaycast = self.arView.session.trackedRaycast(raycastQuery) { results in
// Update Handler's content
guard let result: ARRaycastResult = results.first
else { return }
let anchor = AnchorEntity(world: result.worldTransform)
anchor.addChild(self.scene)
self.arView.scene.anchors.append(anchor)
}
Then you can stop tracking:
repeatRaycast?.stopTracking()