Part 5: Collisions with Moving Objects

Christiane Snyder

Both the spaceship and power-up need to react to when the GvrMain collides with them, so they need collision geometries and rigid bodies.

Add a RigidBody as a new component to the Good prefab, as well as a Box Collider with the following scale and center:


Also attach a RigidBody to the Bad prefab.

We have to take a different approach to GvrMain’s collision with the spaceship. If we add a box collider to the spaceship, it’s rotation may cause the collider to hit the camera’s box collider at an unpredictable distance away from the actual spaceship object, causing it to be destroyed at different times/distances away from the main camera. (some of which may be too close to the camera, which can be very disorienting)


Instead we’ll add a Sphere Collider, which will ensure that the collision between the spaceship and the camera will be always be detected when the spaceship and camera are a set (unvarying) distance from each other. Having a stable distance in which the spaceship collision is detected will also prevent it from reaching and moving through the camera since it will always be deleted at this set distance.


The spaceship obstacle/Bad prefab’s sphere collider should have the following properties:


The GvrMain/Main Camera needs a way to detect and process these collisions, so create a Player.cs script with the following implementation of OnCollisionEnter:


Attach Player.cs  to GvrMain:


Since the collisions with the spaceship will be a bit more complex, we’ll only worry about collisions with power-ups since they’re pretty easy.

Along with the Player script, add a Box Collider component to the GvrMain game object: