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)

                }

            }

        }

    }
Answered by DTS Engineer in 724141022

Hello tzevoon,

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.

This is certainly achievable, the issue in your current code is that you are creating and adding a new child node every time you get a hit result.

My recommendation is that, once you have added this node to the scene once, just change the node's position when you get a new hit result.

Accepted Answer

Hello tzevoon,

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.

This is certainly achievable, the issue in your current code is that you are creating and adding a new child node every time you get a hit result.

My recommendation is that, once you have added this node to the scene once, just change the node's position when you get a new hit result.

ARKit - Limit to single child node only
 
 
Q