Scenekit: rotate camera to tap point (3D)

Viewed 32

I have a camera node. Around the camera node, there is another big node (.obj file) of a building. User can move inside the building. User can do LongPressGesture, and additional node (let's say a sphere) appears on the wall of the building. I want to rotate my camera to this new node (to tap location). I don't know how to do it. Can someone help me? Other answers are not correct for me. Camera just rotates in random directions.

1 Answers

I've found a way!

  1. I take the location of a tap (or any coordinates you need to turn to)
    @objc private func handleLongPress(pressRec: UILongPressGestureRecognizer) {
        let arr: [UIGestureRecognizer.State] = [.cancelled, .ended, .failed]
        if !arr.contains(pressRec.state) {
            let touchPoint = pressRec.location(in: sceneView)
            let hitResults = sceneView.hitTest(touchPoint, options: [:])
            if let result: SCNHitTestResult = hitResults.first {
                createAnnotation(result.worldCoordinates)
                pressRec.state = .cancelled
            }
        }
    }
  1. func for turn camera:
    func turnCameraTo(worldCoordinates: SCNVector3) {
        SCNTransaction.begin()
        SCNTransaction.animationDuration = C.hotspotAnimationDuration
        cameraNode.look(at: worldCoordinates)
        sceneView.defaultCameraController.clearRoll()
        SCNTransaction.completionBlock = {
        }
        SCNTransaction.commit()
    }
Related