How to get a tap position in ARKit?

Viewed 43

I am trying to tap the screen and get tap position then I can fire the bullet from the position I tap.

I searched and tried many ways such as below but it doesn't work

guard let pointOfView = sceneView.pointOfView else { return }

let transform = pointOfView.transform
let orientation = SCNVector3(-transform.m31, -transform.m32, -transform.m33)
let scnView = sender.view as! ARSCNView
let holdLocation = sender.location(in: scnView)
let estimatedPlane: ARRaycastQuery.Target = .existingPlaneInfinite
let alignment: ARRaycastQuery.TargetAlignment = .any
    
guard let query = scnView.raycastQuery(from: holdLocation, 
                                   allowing: estimatedPlane, 
                                  alignment: alignment) 
else { return }
    
let results = scnView.session.raycast(query)
guard let pointResult = results.first else { return }

let pointTransform =  SCNMatrix4(pointResult.worldTransform)

let pointLocation = SCNVector3(pointTransform.m41, 
                               pointTransform.m42, 
                               pointTransform.m43)

let pointPosition = orientation + pointLocation

Can anyone help to tell me how to do it? Highly appreciate it.

0 Answers
Related