Adding touches
Before we can add objects to the scene, we have to include touches so that an object will be placed where we touch on the scene. Basically, wherever we touch on the scene, a ray is cast onto the plane created in the previous step. Wherever the ray touches the plane, we get an intersection. Using this point as reference, we can place objects in the scene.
We will add some Booleans at the top of the viewConctroller
class:
var bGameSetup = false var bGameOver = false
bGameSetup
will check whether the game is ready to start. bGameOver
is a flag we set when the game is over.
To add touches to the scene, add the following function to the ViewController
class:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let location = touches.first!.location(in: sceneView) var hitTestOptions = [SCNHitTestOption: Any]() hitTestOptions[SCNHitTestOption.boundingBoxOnly] = true let hitTestArray...