1

I have the following code in viewDidLoad

let scene = SCNScene(named: "funModel")!
myNode = scene.rootNode.childNode(withName: "blah")!
myNode.position = SCNVector3Make(0, 0, -1)
sceneView.scene = scene
let pan = UIPanGestureRecognizer(target: self, action: #selector(panGesture(recognizer:)))
view.addGestureRecognizer(pan)

and then the following code in panGesture from another SO answer, but it's not working. The node just goes to 0, 0, 0 it seems like. I just want to be able to drag the node around the screen.

let projectedOrigin = sceneView.projectPoint(SCNVector3Zero)
let vp = recognizer.location(in: sceneView)
let vpWithZ = SCNVector3(x: Float(vp.x), y: Float(vp.y), z: Float(projectedOrigin.z))
let worldPoint = sceneView.unprojectPoint(vpWithZ)
myNode.position = worldPoint
rickster
  • 121,850
  • 25
  • 265
  • 319
mergesort
  • 4,921
  • 13
  • 37
  • 63
  • Note that said other answer is specific to moving a node along an imaginary plane perpendicular to the camera; if that's not your intention you'll be getting results you don't want. – rickster Jul 17 '17 at 21:54

1 Answers1

1

You first need to get hittest point from UIPanGestureRecognizer location (in your UIPanGestureRecognizer function) like:

let point = pan.location(in: scene)
let result = scene.hitTest(point, types: .featurePoint)
let resultPoint = result.first

Now your new world point is:

let vpWithZ = SCNVector3(x: Float(resultPoint.worldTransform.columns.3.x), y: Float(resultPoint.worldTransform.columns.3.y), z: Float(projectedOrigin.z))

Hope it helps.

Satish Mavani
  • 4,729
  • 2
  • 18
  • 28