iosvirtual-realityrealitykitvisionos

How to Attach a Camera to a Capsule for First-Person View in visionOS with RealityKit immersive view?


I'm working on a first-person game using visionOS and RealityKit. I'm try to use perspective camera view in order to make a first person game, I'm doing some simple test but I'm unable to to see the view from the camera once I'm in immersive view.

The idea is to attach the camera to the character and move the characters in the map.

I did few try using an ARView app using arView.cameraMode = .nonAR and it worked, but visionOS doesn't have this option. Is there any way to use perspective camera?

So far my test:

 struct Immersive: View {
    @Environment(AppModel.self) private var appModel
    var body: some View {
        RealityView { content in
            await content.add(makeModel())
        }
    }
    
    func makeModel() async -> Entity {
        
        let gameScene = Entity()
        
        let sph = createSphereEntity(radius: 1, color: .red)
        
        let anchor =  AnchorEntity()
        anchor.position = .init(x: 0, y: 0.5, z: -4)
        anchor.addChild(sph)
        gameScene.addChild(anchor)
        
//
        let sph2 = createSphereEntity(radius: 0.5, color: .brown)
        let anchor2 =  AnchorEntity()
        anchor2.position = .init(x: 0.5, y: 0.0, z: -2)
        anchor2.addChild(sph2)
        gameScene.addChild(anchor2)
        
//
        let cameraEntity = Entity()
        let cameraPosition: SIMD3<Float> = [3, -7, -6]
        cameraEntity.look(at: sph2.position, from: cameraPosition, relativeTo: nil)
        cameraEntity.components.set(PerspectiveCameraComponent())
//
        gameScene.addChild(cameraEntity)
        
      
        
        return gameScene
    }
}


Solution

  • As of visionOS 2, PerspectiveCamera does not work in visionOS because the user is the Camera Transform.

    To give the "first-person" game feeling you have to move the world around the user.

    The best open source example I have seen so far is

    https://github.com/talkol/flappy-experience

    This isn't my project just something I found online,

    It is lot of code but they have a "Container" world that spins around below user's head to give the appearance to the user that they are flying.