I haven't found a way to gain access to the camera or player entity from the context of RealityView. In the meantime, I put together a quick demo that moves content in a USDA scene to the player. Move the world instead of the player.
| struct Lab5017: View { |
| @State var selected: String = "Tap Something" |
| @State var sceneContent: Entity? |
| @State var sceneContentPosition: SIMD3<Float> = [0,0,0] |
| |
| var tap: some Gesture { |
| SpatialTapGesture() |
| .targetedToAnyEntity() |
| .onEnded { value in |
| selected = value.entity.name |
| |
| |
| let vectorToTap = value.entity.position |
| |
| |
| let direction = normalize(vectorToTap) |
| |
| |
| let distance = length(vectorToTap) |
| |
| |
| let newPosition = -direction * distance |
| |
| |
| sceneContentPosition.x = newPosition.x |
| sceneContentPosition.z = newPosition.z |
| |
| } |
| } |
| |
| var body: some View { |
| RealityView { content, attachments in |
| |
| if let model = try? await Entity(named: "5017Move", in: realityKitContentBundle) { |
| content.add(model) |
| |
| |
| if let floorParent = model.findEntity(named: "SceneContent") { |
| sceneContent = floorParent |
| sceneContentPosition = floorParent.position |
| } |
| |
| } |
| |
| if let attachmentEntity = attachments.entity(for: "SelectedLabel") { |
| attachmentEntity.position = [0.8, 1.5, -2] |
| attachmentEntity.scale = [5,5,5] |
| content.add(attachmentEntity) |
| } |
| |
| } update: { content, attachments in |
| |
| |
| sceneContent?.position = sceneContentPosition |
| |
| |
| } attachments: { |
| Attachment(id: "SelectedLabel") { |
| Text(selected) |
| .font(.largeTitle) |
| .padding(18) |
| .background(.black) |
| .cornerRadius(12) |
| } |
| } |
| .gesture(tap) |
| } |
| } |