Search code examples
c#unity-game-engineaugmented-realityarkitrealitykit

How to attach 3D object to screen?


How can I make a 3D object to be attached to my phone, so I can move around, and then after I click on the screen - the 3D object should remain in the current position in 3D space?


Solution

  • Native RealityKit approach

    @Rumata, here's how your code might look like for stick-and-drop methodology. A sphere object will be automatically dropped in 5 seconds after app's launch. The same way you can implement Tap Gesture (via Storyboard or programmatically, it's up to you).

    import ARKit
    import RealityKit
    
    class ViewController: UIViewController {
        
        @IBOutlet var arView: ARView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let mesh = MeshResource.generateSphere(radius: 0.1)
            let sphere = ModelEntity(mesh: mesh)
            sphere.transform.translation = [0, 0,-1]
    
            let anchor = AnchorEntity(.camera)
            sphere.setParent(anchor)
            arView.scene.addAnchor(anchor)
                    
            DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                
                let frame = self.arView.session.currentFrame
                let envAnchor = AnchorEntity(world: (frame?.camera.transform)!)
                
                sphere.removeFromParent()
                envAnchor.addChild(sphere)
    
                self.arView.scene.addAnchor(envAnchor)
            }
        }
    }