我在SCNode的命中检测方面遇到了一些问题。我需要检测场景中哪个对象被触摸了,我已经实现了这段代码,但当我触摸该对象时似乎会崩溃,但是当我触摸场景中其他部分时它却工作正常。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first as! UITouch
if(touch.view == self.sceneView){
print("touch working")
let viewTouchLocation:CGPoint = touch.location(in: sceneView)
guard let result = sceneView.hitTest(viewTouchLocation, options: nil).first else {
return
}
if (bottleNode?.contains(result.node))! { //bottleNode is declared as SCNNode? and it's crashing here
print("match")
}
}
}