Post

Replies

Boosts

Views

Activity

ARKit - Limit to single child node only
My app allows a user to place an object by tapping on the screen. It also allows the user the delete It. However, I want to limit to just a single object at any one time. But I cannot seem to do that. It always allow for more than 1 objects to be placed and creates confusion to the user. I've tried adding the object to addChildNode[0] but it doesn't seem to work. I also tried setting var domeArray = [SCNNode?](repeating: nil, count: 1) to a fixed single size array but it didn't work also. Does the community has any experience on this? Below is my code snippet.     var domeArray = [SCNNode]()     @IBOutlet var sceneView: ARSCNView!     @IBAction func removeCompass(_ sender: UIButton) {         if !domeArray.isEmpty {             for dome in domeArray {                 dome.removeFromParentNode()             }         }     }     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {         if let touch = touches.first {             let touchLocation = touch.location(in: sceneView)             guard let query = sceneView.raycastQuery(from: touchLocation, allowing: .existingPlaneInfinite, alignment: .any) else {                 return             }                          let results = sceneView.session.raycast(query)             if let hitResult = results.first {                 let domescene = SCNScene(named: "art.scnassets/DOME.scn")!                 if let domenode = domescene.rootNode.childNode(withName: "Sphere", recursively: true) {                     domenode.position = SCNVector3(                         x: hitResult.worldTransform.columns.3.x,                         y: hitResult.worldTransform.columns.3.y,                         z: hitResult.worldTransform.columns.3.z                     )                     domenode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: -Float.pi/2)                     domeArray.append(domenode)                     sceneView.scene.rootNode.addChildNode(domenode)                 }             }         }     }
1
0
828
Aug ’22