Player Upgrade System in Unity
This tutorial will guide you through creating a basic upgrade system in Unity. Upgrade systems are common in games, allowing players to improve their characters, equipment, or abilities over time. We’ll create an example upgrade system that allows a player to increase attributes like health, speed, and attack power.
Prerequisites
- Unity Editor installed.
- Basic understanding of C# and Unity’s UI system.
- A project set up with a basic player GameObject and a UI Canvas for displaying upgrade options.
Step 1: Set Up Player Attributes
First, create a C# script to define the player’s attributes that can be upgraded. This script will hold properties such as health, speed, and attack power.
using UnityEngine;
public class PlayerAttributes : MonoBehaviour
{
public int health = 100;
public float speed = 5f;
public int attackPower = 10;
public void IncreaseHealth(int amount)
{
health += amount;
Debug.Log("Health increased to " + health);
}
public void IncreaseSpeed(float amount)
{
speed += amount;
Debug.Log("Speed increased to " + speed);
}
public void IncreaseAttackPower(int amount)
{
attackPower += amount;
Debug.Log("Attack Power increased to " + attackPower);
}
}
Attach this PlayerAttributes
script to your player GameObject to manage its attributes.
Step 2: Create the Upgrade Manager
Next, create an UpgradeManager
script to manage upgrade options and apply them to the player’s attributes. This script will allow you to control how much each upgrade costs and how much it increases the player’s attributes.
using UnityEngine;
public class UpgradeManager : MonoBehaviour
{
public PlayerAttributes player;
public int healthUpgradeCost = 50;
public int speedUpgradeCost = 30;
public int attackUpgradeCost = 40;
public void UpgradeHealth()
{
if (CurrencySystem.Instance.SpendCurrency(healthUpgradeCost))
{
player.IncreaseHealth(10); // Increase health by 10
}
}
public void UpgradeSpeed()
{
if (CurrencySystem.Instance.SpendCurrency(speedUpgradeCost))
{
player.IncreaseSpeed(0.5f); // Increase speed by 0.5
}
}
public void UpgradeAttackPower()
{
if (CurrencySystem.Instance.SpendCurrency(attackUpgradeCost))
{
player.IncreaseAttackPower(5); // Increase attack power by 5
}
}
}
This UpgradeManager
script checks if the player has enough currency for each upgrade, then applies the upgrade if affordable. Next, we’ll add a simple currency system for managing upgrade costs.
Step 3: Create a Simple Currency System
We’ll create a basic currency system that tracks the player’s currency and allows them to spend it on upgrades. This can be extended for any in-game economy.
using UnityEngine;
public class CurrencySystem : MonoBehaviour
{
public static CurrencySystem Instance;
public int currency = 100;
private void Awake()
{
if (Instance == null)
Instance = this;
else
Destroy(gameObject);
}
public bool SpendCurrency(int amount)
{
if (currency >= amount)
{
currency -= amount;
Debug.Log("Currency spent: " + amount + ". Remaining: " + currency);
return true;
}
else
{
Debug.Log("Not enough currency.");
return false;
}
}
public void AddCurrency(int amount)
{
currency += amount;
Debug.Log("Currency added: " + amount + ". Total: " + currency);
}
}
This CurrencySystem
script can add and spend currency, and it’s implemented as a singleton so you can easily access it from other scripts. Attach it to a GameObject in the scene.
Step 4: Set Up UI for Upgrades
To let players interact with the upgrade system, create a UI in Unity with buttons for each upgrade option. Here’s how:
- Create a Canvas in your scene (GameObject > UI > Canvas).
- Add Buttons to the Canvas, labeling them as "Upgrade Health," "Upgrade Speed," and "Upgrade Attack".
- Attach the
UpgradeManager
script to a GameObject in the scene, and assign your player GameObject to itsplayer
field in the Inspector. - Link each Button’s On Click event to call the appropriate upgrade function (e.g.,
UpgradeManager.UpgradeHealth()
).
Step 5: Testing the Upgrade System
Run the game, and try pressing the upgrade buttons. Each upgrade should apply to the player’s attributes, and the CurrencySystem
should decrease the currency accordingly. Use the Debug log to confirm that attributes and currency update as expected.
Conclusion
We created a basic upgrade system in Unity, including player attributes, a currency system, and an upgrade manager. You can customize this system to add more upgrades, increase upgrade costs dynamically, or even add new types of upgrades based on your game’s requirements. This foundation can be expanded to create more complex and engaging upgrade mechanics in your games.