I'm new to swift, and I'm trying to get the location of the camera in the ARKit example, but it's giving me nil
.
I used the solution for this question and set the delegate to self
, but it's still not giving me the camera transform.
What am I doing wrong?
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingSessionConfiguration()
sceneView.session.run(configuration)
}
func session(_ session: ARSession, didUpdate frame: ARFrame) {
print("Hello") // DOES NOT RUN HERE
let currentTransform = frame.camera.transform
}
}
Simple fix, first your class definition:
class ViewController: UIViewController, ARSCNViewDelegate, ARSessionDelegate
Then you'll need to set yourself as the delegate like so:
sceneView.session.delegate = self
And then everything's going to work just right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With