I am trying to drag a node exactly where my finger is on the screen on the Y
-axis. But I don't find any way to do it, here is my current code. Do you have any idea?
var movedObject:SCNNode?
@objc func handlePan(_ recognizer: UIPanGestureRecognizer) {
if recognizer.state == .began {
let tapPoint: CGPoint = recognizer.location(in: sceneView)
let result = sceneView.hitTest(tapPoint, options: nil)
if result.count == 0 {
return
}
let hitResult: SCNHitTestResult? = result.first
movedObject = hitResult?.node //.parent?.parent
} else if recognizer.state == .changed {
if (movedObject != nil) {
let tapPoint: CGPoint = recognizer.location(in: sceneView)
let hitResults = sceneView.hitTest(tapPoint, types: .featurePoint)
let result: ARHitTestResult? = hitResults.last
if result?.worldTransform != nil{
let matrix: SCNMatrix4 = SCNMatrix4.init((result?.worldTransform)!)
let vector: SCNVector3 = SCNVector3Make(matrix.m41, matrix.m42, matrix.m43)
movedObject?.position = vector
}
}
} else if recognizer.state == .ended {
movedObject = nil
}
}