Unity: Modular Power Up System Using Lists
I’ve created a power up system for my player that is modular and expandable.
The Power Up Script
this script will hold the database of power ups. This is where we’ll create power ups and where the player will pull all its information from. Ill be breaking this down section by section with a brief explanation.
Class within the script
This is our template of what our power up is. They’re many variables and can be modified through the inspector. Not all variables will be taken or used for every power up created.
I will have the player script specifically grab information based on the,’Category’ of the power up and the,’Type’.
This List is critical. This creates the power ups and stores them in a list to be used later. It’s set to public so that the player script may access it easily.
Power Up Selector
Whenever the power up spawns, the power up will randomly select through the current list of power ups and designate itself as a power up in the list using a integer variable.
It will then set itself a sprite if there is a sprite available.
The Player Script
Here are the sequence of events to when the player touches a power up.
The Player will call a method(CallPowerUp)when it has detected a collider with a tag of PowerUp. Passing it the colliders game object.
Call Power Up Method
Here's the script that will activate specific power ups
If statements are used for specific power ups that are grabbed. There are only two categories of power ups and two types.
Category categorizes power ups into two categories of weapon or utility. Does it do damage or add a player effect.
Type declares is the power up is temporary or permanent. If its temporary then it’ll have a expiration time. Permanent will remain in game as long as the player is alive or until swapped.
In short, weapons will swap out the players current projectile with another prefab.
Utility power ups will call a specific method using invoke and passing it a string from the power up class.
BaseProjectile is used to store the players current permanent weapon.
Laser is what the player will be shooting and will be swapped to any weapon power up grabbed or returned to BaseProjectile when a power up expires.
WeaponTimer is the Duration.
The Player Projectile (Laser)
The players projectile is its own prefab with its own script that holds information such as damage and projectile speed.
If I want to create new weapons, I simply clone the prefab and adjust it from there.
Here is the method that is called when the player has touched a power up with a category of Weapon and type of Temp. Its passed a Projectile to shoot and a timer defined within the class
Weapon Timer Method
This is the method that will return the players original projectile after the weapon power up has ended. It is placed in Update() and is constantly checked.
Permanent weapons call no methods. Its simply assigned directly to BaseProjectile
Utility Power Ups
Utility Powers will call methods through Invoke. A string,’CallMethod’ within the power up class will be passed into the invoke function to call specific player utility functions.
example of power up and their method pairing:
Shield Power up
I can expand the amount of power ups without having to hard code anything in. The only power that has to be type in is the Utilities because those are unique from one another. Whereas weapons all function the same.