My understanding is that those cameras are not relevant for visionOS development. Those are used on iOS and other platforms.
When I tried to do this last winter it seemed that the only answer was to move the world around the user, instead of moving the user around the world.
Here is an excerpt from from the code I came up with. This allows as user to tap on an entity and move to a new position. Sort of like "waypoint" teleportation that was common in VR games circa 2016-2017. This could be improved in lots of ways. For example, using SpatialEventGesture or SpatialTapGesture to get a more precise location.
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
// Calculate the vector from the origin to the tapped position
let vectorToTap = value.entity.position
// Normalize the vector to get a direction from the origin to the tapped position
let direction = normalize(vectorToTap)
// Calculate the distance (or magnitude) between the origin and the tapped position
let distance = length(vectorToTap)
// Calculate the new position by inverting the direction multiplied by the distance
let newPosition = -direction * distance
// Update sceneOffset's X and Z components, leave Y as it is
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)
// Get the scene content and stash it in state
if let floorParent = model.findEntity(named: "SceneContent") {
sceneContent = floorParent
sceneContentPosition = floorParent.position
}
}
//Position the attachment somewhere we can see it
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
// Update the position of scene content anytime we get a new position
sceneContent?.position = sceneContentPosition
} attachments: {
Attachment(id: "SelectedLabel") {
Text(selected)
.font(.largeTitle)
.padding(18)
.background(.black)
.cornerRadius(12)
}
}
.gesture(tap) // The floor child entities can receive input, so this gesture will fire when we tap them
}
}