We continued a small amount of work on our asteroid dodging game today, although it really was only a quick update to denote win/lose conditions – hence why I’ve decided to dub this post part 2.5 rather than 3. I’d come across a number of collision and trigger scripts while researching ideas for my Unity room game, so I was already comfortable with what we were covering today. I actually think triggers and collisions are some of the best things to include in a game, since they can be unpredictable (like jump scares) or extremely useful (like a prompt to load the next level of the game).
Since we know our ships will be dodging asteroids in space, we don’t want them to get hit otherwise the player dies. Thus, we needed a way to tell Unity that, if the player’s ship hits an asteroid, it should be game over and the level should reload. We could do this by calling on collisions. Unlike in Unity 3D, Unity 2D uses slightly different wording:
Seeing this code, we also needed to put in a new namespace called UnityEngine.SceneManagement – this would allow us to change/refer to various scenes in our project. What the above code will do is, once we attach colliders to both our ship and asteroids, once they meet the scene will reload from the beginning (as if the ship has been destroyed).
While there are a number of colliders in Unity, as seen above, the best one for our game was the polygon collider. This will try its hardest to trace the polygons within the object, however if it’s not quite to your liking you can alter it by using the “Edit Collider” option in the polygon collider component; this is what I did for the ship, so that the lines matched the outline as closely as possible.
The same can be done for the asteroid we have floating in our scene and this is all it takes for the colliders to work! As Ant pointed out in class, it doesn’t take much to turn a collider into a trigger (which will still need to have a collider on it, so the box next to “Is Trigger” in the component is ticked).
By adding this small bit of code, the game would load the next level we tell it to (after adding a public string to the script named nextLevel).
As you can see here, we would have to put in the next level’s name in the component, but this means that when the ship enters the trigger it will denote the level has been won and the player can advance.
I created a second scene to test and all of it works correctly.
For an added bonus, I added some craggy peaks to either side of the game window, so it felt like you were doing a canyon run while avoiding obstacles.
Here you can see the ship moves around the scene and, if it knocks into a wall it will restart the level. Using the asteroid as a trigger, however, and the player passes through it, the next level will load instead.
I really like these game mechanics and would love to continue using them in future projects. I feel like there are so many possibilities for both 2D and 3D games, so I’m glad for the continued practice with quick updates like these.