- Killboxes
- 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!
Killboxes
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.
Respawning
As you can see in the code above when the player collides with a “Kill” object it calls the KillPlayer function from the gamemasterScript.
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
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.
PowerUp Activators
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: