Combat Setup Tutorial
Learn how to set up the turn-based combat system for your 2D RPG.
Combat System Overview
The 2D RPG Adventure Game System features a turn-based combat system with the following components:
Turn-Based Combat
Players and enemies take turns performing actions
Multiple Actions
Attack, use skills, consume items, or attempt to flee
Visual UI
Comprehensive battle interface with health bars and action buttons
Experience & Rewards
Gain experience and loot after successful battles
Setting Up Combat Manager
Step 1: Create Combat Manager
The Combat Manager is automatically created by the Setup Wizard, but you can create one manually:
1"comment">// Create Combat Manager GameObject
2GameObject combatManager = "keyword">new GameObject("CombatManager");
3CombatManager combatComponent = combatManager.AddComponent<CombatManager>();
4
5"comment">// Configure Combat Manager
6combatComponent.turnDelay = 1f; "comment">// Delay between turns
7combatComponent.battleUI = battleUIComponent; "comment">// Assign Battle UI
Step 2: Configure Combat Settings
Select the Combat Manager and configure its properties:
| Property | Description | Default |
|---|---|---|
| Turn Delay | Time between turns (seconds) | 1.0 |
| Battle UI | Reference to the Battle UI component | None |
| Player Stats | Reference to player's stats | Auto-assigned |
Step 3: Starting Combat
Initiate combat from your exploration code:
1"comment">// Start combat with a list of enemies
2"keyword">public "keyword">void StartCombatEncounter()
3{
4 List<Enemy> enemies = "keyword">new List<Enemy>();
5
6 "comment">// Add enemies to the encounter
7 Enemy goblin = CreateEnemy("Goblin", goblinData);
8 Enemy orc = CreateEnemy("Orc", orcData);
9
10 enemies.Add(goblin);
11 enemies.Add(orc);
12
13 "comment">// Start the combat
14 CombatManager.Instance.StartCombat(enemies);
15}
16
17Enemy CreateEnemy("keyword">string name, EnemyData data)
18{
19 GameObject enemyObj = "keyword">new GameObject(name);
20 Enemy enemy = enemyObj.AddComponent<Enemy>();
21 enemy.enemyData = data;
22 enemy.currentHealth = data.maxHealth;
23 "keyword">return enemy;
24}
Enemy Setup
Step 1: Create Enemy Data
Create ScriptableObject assets for your enemies:
- Right-click in Project window
- Go to
Create > RPG > Enemy - Name your enemy (e.g., "Goblin")
- Configure the enemy properties
1"comment">// Example Enemy Data configuration
2[CreateAssetMenu(fileName = "New Enemy", menuName = "RPG/Enemy")]
3"keyword">public "keyword">class EnemyData : ScriptableObject
4{
5 "keyword">public "keyword">string enemyName = "Goblin";
6 "keyword">public Sprite sprite;
7
8 [Header("Stats")]
9 "keyword">public "keyword">int maxHealth = 50;
10 "keyword">public "keyword">int attack = 8;
11 "keyword">public "keyword">int defense = 2;
12 "keyword">public "keyword">int agility = 5;
13
14 [Header("Rewards")]
15 "keyword">public "keyword">int experienceReward = 25;
16 "keyword">public LootTable lootTable;
17}
Step 2: Configure Enemy Behavior
Set up enemy AI patterns:
| Behavior Type | Description | Use Case |
|---|---|---|
| Aggressive | Always attacks the player | Basic melee enemies |
| Defensive | Sometimes defends or heals | Tank-type enemies |
| Magic User | Uses spells and abilities | Mage-type enemies |
Step 3: Loot Tables
Create loot tables for enemy rewards:
- Right-click in Project window
- Go to
Create > RPG > Loot Table - Add items with drop chances
- Assign to enemy data
1"comment">// Example loot table configuration
2[System.Serializable]
3"keyword">public "keyword">class LootItem
4{
5 "keyword">public Item item; "comment">// The item to drop
6 "keyword">public "keyword">int quantity = 1; "comment">// How many to drop
7 [Range(0f, 1f)]
8 "keyword">public "keyword">float dropChance = 0.5f; "comment">// 50% chance to drop
9}
10
11"comment">// In LootTable ScriptableObject
12"keyword">public LootItem[] items = {
13 "keyword">new LootItem { item = goldCoin, quantity = 5, dropChance = 0.8f },
14 "keyword">new LootItem { item = healthPotion, quantity = 1, dropChance = 0.3f },
15 "keyword">new LootItem { item = ironSword, quantity = 1, dropChance = 0.1f }
16};
Battle UI Setup
Step 1: Create Battle UI Canvas
Set up the combat interface:
- Create a new Canvas for battle UI
- Add the BattleUI component
- Create child objects for UI elements
Battle Canvas ├── Player Info Panel │ ├── Player Health Bar │ ├── Player Mana Bar │ └── Player Portrait ├── Enemy Info Panel │ └── Enemy Health Bars (Dynamic) ├── Action Panel │ ├── Attack Button │ ├── Skill Button │ ├── Item Button │ └── Run Button ├── Combat Log └── Victory/Defeat Panels
Step 2: Configure UI References
Assign UI references in the BattleUI component:
1"keyword">public "keyword">class BattleUI : MonoBehaviour
2{
3 [Header("Player UI")]
4 "keyword">public Slider playerHealthBar;
5 "keyword">public Slider playerManaBar;
6 "keyword">public Text playerHealthText;
7 "keyword">public Text playerManaText;
8
9 [Header("Action Buttons")]
10 "keyword">public Button attackButton;
11 "keyword">public Button skillButton;
12 "keyword">public Button itemButton;
13 "keyword">public Button runButton;
14
15 [Header("Panels")]
16 "keyword">public GameObject victoryPanel;
17 "keyword">public GameObject defeatPanel;
18
19 "comment">// Events "keyword">for combat actions
20 "keyword">public System.Action OnPlayerAction;
21 "keyword">public System.Action<Enemy> OnAttackEnemy;
22}
Combat Flow
Turn-Based Combat Sequence
Understanding the combat flow:
Combat Start
Initialize combat, show battle UI
Player Turn
Show action buttons, wait for input
Execute Action
Process player's chosen action
Enemy Turn
Each enemy performs their action
Check Victory
Determine if combat should end
Implementing Custom Actions
Add custom combat actions:
1"comment">// Add a custom skill action
2"keyword">public "keyword">void UseFireball()
3{
4 "comment">// Check "keyword">if player has enough mana
5 "keyword">if (playerStats.mana.Value >= 15)
6 {
7 "comment">// Use mana
8 playerStats.UseMana(15);
9
10 "comment">// Apply damage to all enemies
11 "keyword">foreach (var enemy in enemies)
12 {
13 "keyword">if (enemy.IsAlive())
14 {
15 "keyword">int damage = playerStats.CalculateSpellDamage(20);
16 enemy.TakeDamage(damage);
17
18 "comment">// Show damage effect
19 battleUI.ShowDamageNumber(enemy.transform.position, damage);
20 }
21 }
22
23 "comment">// End player turn
24 OnPlayerAction?.Invoke();
25 }
26}
Testing Combat
Step 1: Create Test Scene
Set up a scene for testing combat:
- Create a new scene
- Add GameManager with CombatManager
- Add Player with PlayerStats
- Add Battle UI Canvas
- Create test enemies
Step 2: Test Combat Scenarios
Create a test script to try different combat situations:
1"keyword">public "keyword">class CombatTester : MonoBehaviour
2{
3 "keyword">public EnemyData[] testEnemies;
4
5 "keyword">void Update()
6 {
7 "comment">// Press keys to test different scenarios
8 "keyword">if (Input.GetKeyDown(KeyCode.Alpha1))
9 {
10 TestSingleEnemy();
11 }
12 "keyword">else "keyword">if (Input.GetKeyDown(KeyCode.Alpha2))
13 {
14 TestMultipleEnemies();
15 }
16 "keyword">else "keyword">if (Input.GetKeyDown(KeyCode.Alpha3))
17 {
18 TestBossEncounter();
19 }
20 }
21
22 "keyword">void TestSingleEnemy()
23 {
24 List<Enemy> enemies = "keyword">new List<Enemy>();
25 enemies.Add(CreateEnemy(testEnemies[0]));
26 CombatManager.Instance.StartCombat(enemies);
27 }
28
29 "keyword">void TestMultipleEnemies()
30 {
31 List<Enemy> enemies = "keyword">new List<Enemy>();
32 enemies.Add(CreateEnemy(testEnemies[0]));
33 enemies.Add(CreateEnemy(testEnemies[1]));
34 enemies.Add(CreateEnemy(testEnemies[0]));
35 CombatManager.Instance.StartCombat(enemies);
36 }
37}
Step 3: Debug Combat Issues
Common debugging techniques:
🐛 Combat Not Starting
Check that CombatManager.Instance is not null and enemies list is populated
🐛 UI Not Responding
Verify Button onClick events are properly assigned and EventSystem exists
🐛 Damage Not Applied
Check damage calculations and ensure TakeDamage method is called
Combat Best Practices
⚖️ Balance Combat
Test different enemy configurations to ensure fair but challenging encounters
🎨 Visual Feedback
Add particle effects, screen shake, and sound effects for engaging combat
📊 Player Progression
Ensure combat rewards scale with player level and difficulty
🔄 Variety
Create different enemy types and abilities to keep combat interesting