Tower Defense Tutorial - Brackeys - Ep. 27
March 7, 2019
Tower Defense Tutorial
Episode 27 – Unlock Levels
Youtube - How to make a Tower Defense Game (E27 UNLOCK LEVELS) - Unity TutorialBy: Brackeys
Ep. 27
This tutorial gets into creating a system for locking/unlocking levels.
We started by going into our LevelSelector script and creating an array of UI Buttons. This would hold all of the buttons that allow you to select a specific level on the level select screen. We then added a for loop to go through all of these buttons and intitialize them as non-interactible buttons.
To go along with making the buttons non-interactible, we added a Button Disable animation. This was a simple animation that makes the button much more faded out than normal to make it clear that it is non-interactible. To make sure this effect is removed in an interactable state, we added an alpha animation for every other button animation state to just set the alpha back to 1.
Next we needed a way to keep track of the player’s progress to determine what levels they have access to. We wanted this to persist even if the player closes the game, so static variables are not enough. To do this, we need to save data out to a file. We used Unity’s player prefs as the simplest way to do this. We started by creating an int levelReached, which uses PlayerPrefs to GetInt levelReached. When using a get variable command, you also specific a default value to use if there is nothing associated with this value currently (i.e. this is the first time the player is playing the game), so we set that to one so the player will always have access to level 1 initially.
Now we setup that initial for loop that was just disabling every button to only disable buttons if their index (+1) is greater than the levelReached variable. The levelReached variable is now set at the end of a level if the player completes it, increasing the possible level options.
In testing the level select setup, they discovered the bug I mentioned as a possible issue in a previous tutorial. You can sometimes get a scenario where the enemiesAlive in the middle of a wave reaches 0, which signals to our system that the wave is over, even though there are actually more enemies left in the wave. They ended up using my suggested solution as well: just set the enemiesAlive variable equal to the total number of enemies in the wave at the very start of the wave.
They then go over some issues with modifying the game in its current setup. The changes we made in the Game Manager only apply to the current scene, so they have to be redone in every other level (or since we don’t have anything yet, delete the current ones and duplicate the updated level scene). The simple work around for this was to make objects like the GameManager a prefab so when you update it in one scene, it will be updated for every scene. Then, for an even stronger method they suggest is that Unity allows you to put everything that is repeated in each level into its own scene, and only the objects that actually change between levels are their own scene.
SUMMARY
- Look into serialization for saving player data; many more advanced games use .json or .xml to store player date
- Unity uses Player Prefs to do a very simple version of saving data
- When using get/set variables, make sure to use the EXACT SAME identifier name for the variable. This will not generally return an error if entered incorrectly as it will just set it to the default value instead.
- Unity has a system to let you load different scenes in at once, look into properly using this feature (this was the suggested ideal method if you want to create a lot of levels)
Comments
Post a Comment