When I touch the screen I Use the raycast to place a object (entity and anchor) to it's worldTransform
Then I try to touch this object to get it's anchor (or it's own entity)
I am trying to find previously placed anchor with raycast
(or hitTest
) but every things returns nil
like that :
This is my onTap
code :
@IBAction func onTap(_ sender: UITapGestureRecognizer){
let tapLocation = sender.location(in: arView)
guard let result = arView.raycast(from: tapLocation, allowing: .estimatedPlane, alignment: .any).first else { return }
print("we have anchor??")
print(result.anchor) // always nil
// never hit
if let existResult = arView.hitTest(tapLocation).first {
print("hit test trigger")
if let entity = existResult.entity as Entity? {
NSLog("we have an entity (entity.name)")
...
}
}
}
And this is the way I create some object and anchors :
let anchor = AnchorEntity(world: position)
anchor.addChild(myObj)
arView.scene.anchors.append(anchor)
// my obj is now visible
Do you have any idea why I can't manage to get the anchor I touch ?
EDIT :
ARview
configuration :
arView.session.delegate = self
arView.automaticallyConfigureSession = true
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal, .vertical]
NSLog("FINISHED INIT")
if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
config.sceneReconstruction = .mesh // .meshWithClassification
arView.environment.sceneUnderstanding.options.insert([.occlusion])
arView.debugOptions.insert(.showSceneUnderstanding)
NSLog("FINISHED with scene reco")
} else {
NSLog("no not support scene Reconstruction")
}
let tapGesture = UITapGestureRecognizer(target: self, action:#selector(onTap))
arView.addGestureRecognizer(tapGesture)
arView.session.run(config)
question from:
https://stackoverflow.com/questions/65844655/realitykit-arkit-find-an-anchor-or-entity-from-raycast-always-nil 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…