Sam Bing
Sam Bing

Reputation: 2872

Detect touch on child node of object in SceneKit

Basically this question but for SceneKit.

I have a parent node with a few smaller nodes inside it, the parent node at a later point becomes transparent, (parent's diffuse material opacity is set to 0) after that i would like to get the node that was tapped inside the object, how should i go about doing that? The default hit testing returns the parent node, and since there are quiet a few smaller nodes inside the object, i need the precise one that was tapped.

Upvotes: 1

Views: 1706

Answers (1)

Yevhen
Yevhen

Reputation: 1068

To fix this issue I recommend to read next topic from Apple:

https://developer.apple.com/documentation/scenekit/scnhittestoption

General idea:

func registerGestureRecognizer() {
    let tap = UITapGestureRecognizer(target: self, action: #selector(search))
    self.sceneView.addGestureRecognizer(tap)
}

@objc func search(sender: UITapGestureRecognizer) {

    let sceneView = sender.view as! ARSCNView
    let location = sender.location(in: sceneView)
    let results = sceneView.hitTest(location, options: [SCNHitTestOption.searchMode : 1])

    guard sender.state == .ended else { return }

        for result in results.filter( { $0.node.name == "Your node name" }) {
            // do manipulations
    }
}

Hope it helps! Best regards.

Upvotes: 5

Related Questions