Creating a Clash of Clans-like Game in Unity Pt. 3
In this third part of our tutorial series, we will implement a combat system to enable players to engage with other players or AI. This includes managing troop interactions, health, combat animations, and the overall battle mechanics.
Enhancing the Troop Class
We need to enhance the existing Troop class to handle combat better. This will include tracking health and handling attacks from both players and AI.
using UnityEngine;
public class Troop : MonoBehaviour
{
public float movementSpeed = 2f;
public int health = 50; // Added health property
public int damage = 10;
public float attackRange = 1f;
private GameObject target;
void Update()
{
if (target != null)
{
MoveTowardsTarget();
}
}
public void SetTarget(GameObject newTarget)
{
target = newTarget;
}
private void MoveTowardsTarget()
{
float step = movementSpeed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, target.transform.position, step);
if (Vector2.Distance(transform.position, target.transform.position) < attackRange)
{
Attack();
}
}
private void Attack()
{
// Attack the target
Building building = target.GetComponent();
if (building != null)
{
building.TakeDamage(damage);
Debug.Log(name + " attacked " + target.name);
}
}
public void TakeDamage(int damage)
{
health -= damage;
Debug.Log(name + " takes " + damage + " damage.");
if (health <= 0)
{
Destroy(gameObject);
Debug.Log(name + " destroyed!");
}
}
}
Creating a Combat Manager
We will create a CombatManager that will handle the interactions between troops, including detecting when troops are in range to attack and managing their targets.
using UnityEngine;
using System.Collections.Generic;
public class CombatManager : MonoBehaviour
{
public List playerTroops;
public List enemyTroops;
void Update()
{
foreach (Troop troop in playerTroops)
{
FindTarget(troop, enemyTroops);
}
foreach (Troop troop in enemyTroops)
{
FindTarget(troop, playerTroops);
}
}
private void FindTarget(Troop troop, List enemyTroops)
{
foreach (Troop enemy in enemyTroops)
{
if (Vector2.Distance(troop.transform.position, enemy.transform.position) < troop.attackRange)
{
troop.SetTarget(enemy.gameObject);
return; // Exit after setting the first target
}
}
troop.SetTarget(null); // No target found
}
}
Implementing AI Behavior
To make the combat more engaging, we can implement basic AI behavior for enemy troops.
public class EnemyAI : MonoBehaviour
{
public float patrolRange = 5f;
private Vector2 startPosition;
void Start()
{
startPosition = transform.position;
}
void Update()
{
// Simple patrol logic
transform.Translate(Vector2.right * Mathf.Sin(Time.time) * Time.deltaTime);
// Check if the troop is in range to attack
Troop troop = GetComponent();
if (troop != null && troop.target == null)
{
// Find a new target
CombatManager combatManager = FindObjectOfType();
combatManager.FindTarget(troop, combatManager.playerTroops);
}
}
}
Creating Battle Animations
To enhance the visual appeal of combat, we can add simple animations to our troops. You can use Unity's Animator component to set up basic attack animations.
- Import your troop animations into Unity.
- In the Troop class, create a public Animator variable.
- Trigger the animation during the Attack method.
public class Troop : MonoBehaviour
{
public Animator animator; // Add this line
private void Attack()
{
animator.SetTrigger("Attack"); // Trigger attack animation
// ... rest of the attack logic
}
}
Creating a Combat UI
We will implement a simple UI to display health bars for troops and other relevant combat information.
- In the Hierarchy, create a new UI > Canvas.
- Add UI > Image elements to represent health bars for each troop.
- Use a script to update the health bars based on the troop's health.
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
public Troop troop;
public Image healthBar;
void Update()
{
float healthPercentage = (float)troop.health / 50; // Assuming max health is 50
healthBar.fillAmount = healthPercentage;
}
}
Conclusion
In this tutorial, we have added a combat system that allows players to engage with other players or AI. We implemented troop interactions, health management, combat animations, and UI for health status. This forms the basis for a more interactive and strategic gameplay experience in your Clash of Clans-like game.