RealityKit – How to get the name of a node when the user taps on it?

Viewed 33

This is how my nodes and 3d model looks like: enter image description here

What I would like to accomplish is when I tap on the model when the app is running, I want to print out the tapped node's name to the console.

This is what I managed to do but it only gives me back the entity and not the nodes.

@objc private func handleTap(sender: UITapGestureRecognizer) {

    let tapLocation: CGPoint = sender.location(in: arView)
    let result: [CollisionCastHit] = arView.hitTest(tapLocation)

    guard let hitTest: CollisionCastHit = result.first
    else { return }

    let entity: Entity = hitTest.entity
}
1 Answers

You can get the name of a node using name instance property:

print(collisionCastHit.entity.name)

Look at this post for details.

P. S.

raycast(...) and hitTest(...) methods ignore entities that lack a CollisionComponent.

Related