I placed node1 to sceneview.I also added node2 as childnode to node1 when tapped on node1.But I am not able to place node2 correctly at tap location on node1.How can I place it exactly at tap location of scn node in arkit.
Final Code
let sceneView = sender.view as! ARSCNView
let tapLocation = sender.location(in: sceneView)
let hitTest = sceneView.hitTest(tapLocation, options: [:])
guard let node = hitTest.first?.node.parent
if let hitTest = hitTest.first
{
guard let scene = SCNScene(named: "furnitures.scnassets/(furnitureName).scn") else{return}
let childnode = (scene.rootNode.childNode(withName: furnitureName + " " + "parentnode", recursively: false))!
node.addChildNode(childnode)
childnode.position = hitTest.localCoordinates
childnode.scale = SCNVector3(0.5,0.5,0.5)
}
You need to use SCNHitTestResult
localCoordinates
or worldCoordinates
this depend of which one is better for your problem, then you can add a node on that position
this is a example code
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let scnView = self.view as! SCNView
let touch = touches.first
if let point = touch?.location(in: scnView) {
let hitResults = scnView.hitTest(point, options: nil)
if let result: SCNHitTestResult = hitResults.first {
let position = result.localCoordinates
let position2 = result.worldCoordinates
}
}
}