Currently, ray casting is not supported by VisionOS. Is there a workaround where I could get a similar result that is supported by VisionOS?
Raycast available for VisionOS?
Raycasting is supported on visionOS, here is a full example to demonstrate that:
import SwiftUI
import RealityKit
struct ImmersiveView: View {
let entity = ModelEntity(mesh: .generateSphere(radius: 0.1))
var body: some View {
RealityView { content in
entity.generateCollisionShapes(recursive: false)
entity.components.set(InputTargetComponent())
entity.position = [0, 1, -1]
content.add(entity)
}
.gesture(tapGesture)
}
private var tapGesture: some Gesture {
TapGesture()
.targetedToAnyEntity()
.onEnded { value in
let scene = value.entity.scene!
let hits = scene.raycast(from: [0, 1, 0], to: [0, 1, -2])
guard !hits.isEmpty else {
print("No hits.")
return
}
for hit in hits {
print("Hit distance:", hit.distance)
}
}
}
}
I have a visionOS app that shows an immersive space. All my ModelEntities are programmatically created. I tried your code, but my entities have as property scene
always nil,
so I cannot use your code. How is the scene
property set?