Hi @AMobileDeveloper
@igerard is correct. You need to generate collision shapes using SceneReconstructionProvider so the raycast has something to collide with.
Here's a snippet that shows how to create the collision shapes.
| import SwiftUI |
| import RealityKit |
| import ARKit |
| |
| struct ImmersiveView: View { |
| @State private var root = Entity() |
| @State private var meshEntities:Dictionary<UUID, ModelEntity> = [:] |
| @State private var arKitSession = ARKitSession() |
| @State private var sceneReconstruction = SceneReconstructionProvider() |
| |
| var body: some View { |
| RealityView { content in |
| content.add(root) |
| } |
| .task { |
| try? await arKitSession.run([sceneReconstruction]) |
| |
| for await update in sceneReconstruction.anchorUpdates { |
| let meshAnchor = update.anchor |
| |
| guard let shape = try? await ShapeResource.generateStaticMesh(from: meshAnchor) else { continue } |
| switch update.event { |
| case .added: |
| let entity = ModelEntity() |
| entity.transform = Transform(matrix: meshAnchor.originFromAnchorTransform) |
| entity.collision = CollisionComponent(shapes: [shape], isStatic: true) |
| |
| meshEntities[meshAnchor.id] = entity |
| |
| root.addChild(entity) |
| case .updated: |
| guard let entity = meshEntities[meshAnchor.id] else { continue } |
| entity.transform = Transform(matrix: meshAnchor.originFromAnchorTransform) |
| entity.collision?.shapes = [shape] |
| case .removed: |
| meshEntities[meshAnchor.id]?.removeFromParent() |
| meshEntities.removeValue(forKey: meshAnchor.id) |
| } |
| } |
| } |
| } |
| } |
Make sure to add NSWorldSensingUsageDescription
to your app’s information property list to provide a usage description that explains how your app uses the data these sensors provide. People see this description when your app prompts for access to camera data.
Please accept this response if it answers your question or followup so I can help you achieve your goal.