1

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
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
pistifeju
  • 95
  • 8

1 Answers1

0

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.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    Its not working. entity.name gives me back an empty string. – pistifeju Sep 25 '22 at 16:06
  • 1) Entity must have a name (not an empty string), 2) Entity must have a collision component – `entity.generateCollisionShapes(recursive: true)`. – Andy Jazz Sep 25 '22 at 16:14
  • https://imgur.com/a/LrcJaBo This is how my USDZ model looks like right now, and what I want to do is when the user taps on one of the node, for example on the picture, if the person taps on the "Humerus_1" node, it prints out the node's name. – pistifeju Sep 25 '22 at 16:27
  • Print an hierarchy of your `usdz` model (with `.name` instance property) – like this https://stackoverflow.com/questions/56736645/hittest-prints-ar-entity-name-even-when-i-am-not-tapping-on-it/59866900#59866900, or like this – https://stackoverflow.com/questions/62532680/realitykit-what-is-steelbox-instance-loading/62548756#62548756 – Andy Jazz Sep 25 '22 at 16:34
  • But I don't want the hierarchy, I want to know which part of the model the user exactly tapped on. – pistifeju Sep 25 '22 at 16:58
  • Then each part (I mean separate part) of your `usdz` model should be a ModelEntity with a `collision component` and a non-empty string for the `name` property. https://stackoverflow.com/questions/68404663/realitykit-hiding-and-showing-parts-meshes-of-usdz-model/68411215#68411215 – Andy Jazz Sep 25 '22 at 17:03