Find a tapped childNode in 3D object in RealityKit

How can I access the exact child node's name that the user tapped on? So for example, if the user taps on the Humerus_l node I could print it's name to the console.

This is how my 3D USDZ model and its Scene graph looks like. I can also convert it into a .rcproject file but I couldn't make it work with that either.

Hello,

You can hitTest your scene to determine the nearest entity that was hit (assuming this entity has an adequate collision shape), for example:

class GameViewController: NSViewController {
    
    @IBOutlet var arView: ARView!
    
    override func awakeFromNib() {
        
        let boxMesh = MeshResource.generateBox(size: 0.1)
        
        let anchor = AnchorEntity()
        anchor.name = "Anchor"
        
        // Creates a hierarchy of 10 boxes.
        var previousEntity: Entity = anchor
        for index in 1...10 {
            
            let model = ModelEntity(mesh: boxMesh)
            model.name = String("Box \(index)")
            
            model.transform.translation = .init(0.2, 0, 0)
            
            previousEntity.addChild(model)
            previousEntity = model
        }
        
        anchor.generateCollisionShapes(recursive: true)
        
        // Adds the hierarchy to the scene.
        arView.scene.addAnchor(anchor)

        arView.addGestureRecognizer(NSClickGestureRecognizer(target: self, action: #selector(clicked(_:))))        
    }
    
    @objc
    func clicked(_ sender: NSClickGestureRecognizer) {
        
        // Hit-tests the scene at the provided view location, returning only the closest hit collision shape. 
        let results = arView.hitTest(sender.location(in: arView), query: .nearest)
        
        guard let firstHitEntity = results.first?.entity else { return }
        
        // Logs the name of the hit entity.
        print(firstHitEntity.name)
    }
}

But I want to get the name of the tapped children ModelEntity, because my model is made of several other smaller objects. If i print the tapped object children, this is what I get. And I want to get the name of the tapped children not the main object.

That output indicates that your top-level 'arm' Entity has a collision shape that bounds all of its children.

Try setting arView.debugOptions = .showPhysics, and then post a screenshot of what your scene looks like with the collision shape visualization.

Find a tapped childNode in 3D object in RealityKit
 
 
Q