With the revamping of the Platform prefabs I was set on adding the rest of the game’s level building blocks:
- Respawning (inc GameMaster obj and sorting the camera)
- Ending the level
- PowerUps Activators
In addition I also created a tutorial level that showcases the above off and everything i’ve done so far!
A standard mechanic in platformers are hazards the player must avoid, wheter it be spikes or (for now) red boxes in our game.
To do this I simply created a new kill gameObject that had the tag “Kill” as well as the usually Trigger collider and added the following code in the playerScript.
Once the player dies (for now it’s all instant-kill) they need to respawn. For this I needed to create the _GameMaster object, essentially what handles the running and utilities of the game.
As you can see in the code above when the player collides with a “Kill” object it calls the KillPlayer function from the gamemasterScript.
Once the player is killed I added a delayed function (via coroutines) that handles respawning – in this case a new instance of the player is created at the spawnPoint game obj’s location.
If I were to add multiple spawn points I would have to handle saving the player’s current instance (so we know eg what powerups they have or even their health in the future if needed) and respawn that instead – but for now we have this.
If the game also to fit the casual market we wouldn’t really need multiple spawners as each ”encounter’ could essentially be a level on its own.
With the new changes I also had to change the way the camera followed the player.
In the old version the camera was simply a child object that belonged to the player, a rather simple and blunt way of doing it.
This came to a head when implementing respawning as the main camera would also be destroyed when the player died which caused some issues, namely the camera not being able to render anymore as our only viewport into the game is destroyed.
To fixed this I used Unity’s StandardAsset’s Camera2DFollow script which accomplished the same thing as before but kept the camera independent of the player.
I also added some new code in the existing script below:
Ending the Level
This one works exactly like the Kill code, if the player collides with an object tagged “EndLevel” then the current scene will be reloaded.
Right now this is a temp measure, in the future this would be linked up to a GUI for example, asking if the player wants to restart or go back to the menu, etc.
I had several ideas on how to implement the usage of PowerUps (eg Parachutes, Rope, Spikes). One of the ideas was to have a inventory-like system where the player can select which one they used but this required going into GUI so I decided on a much simpler and arcade-y option = collectible powerups.
In my current version the player can only have one powerup at a time (to minimize any unforeseen conflict between them that might arise). When the player collects a certain powerup, all other powerups are disabled. The way I made powerups was to make them individual gameObject children to the player but instead of adding and deleting powerups (which might cause issues with instantiated clones) I opted to just activating and disabling the existing children.
How the PowerUp system works
- player has all powerups as children gameObjects
- if a powerup activator is collected the relevant gameObject is activated and all others are deactivated
- when a gameObject is deactivated this means its sprite graphics appear and its component scripts can happen
I had a bit of an issue doing this as again I tried to do it the simplest way possible (and Unity apparently requires jumping through hoops) as seen in the code below:
Apparently you need to make an object reference first, you can’t just find an object with the name and use it straight away which is perfectly understandable.
Instead of working with tags like before I opted to just use the object’s name as the search criteria as making it tag-based could easily lead to spam with lots of tags if I decide to add new powerups.