Creating a Clash of Clans-like Game in Unity Pt. 2
In this second part of our tutorial series, we will implement troop mechanics for attacking and defending in our Clash of Clans-like game. We will create troop units, manage their movement and behavior, and allow players to deploy them during attacks.
Setting Up Troop Prefabs
First, we need to create troop prefabs that represent different units in the game.
- Create a new GameObject for your troop by right-clicking in the Hierarchy and selecting 2D Object > Sprite.
- Name it Warrior and assign a sprite from your assets.
- Attach a new script called Troop to handle troop logic.
using UnityEngine;
public class Troop : MonoBehaviour
{
public float movementSpeed = 2f;
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()
{
// Logic for attacking the target
Debug.Log("Attacking " + target.name);
}
}
Creating a Troop Manager
We will create a Troop Manager that handles troop deployment and manages active troops on the battlefield.
using System.Collections.Generic;
using UnityEngine;
public class TroopManager : MonoBehaviour
{
public GameObject troopPrefab;
private List activeTroops = new List();
public void DeployTroop(Vector3 position)
{
GameObject troopObject = Instantiate(troopPrefab, position, Quaternion.identity);
Troop troop = troopObject.GetComponent();
activeTroops.Add(troop);
}
void Update()
{
// Here we can handle troop behaviors or remove them if needed
for (int i = activeTroops.Count - 1; i >= 0; i--)
{
if (activeTroops[i] == null)
{
activeTroops.RemoveAt(i);
}
}
}
}
Implementing the Attack Mechanic
For attacking, we will create a basic system where troops can attack buildings or other units.
using UnityEngine;
public class Building : MonoBehaviour
{
public int health = 50;
public void TakeDamage(int damage)
{
health -= damage;
Debug.Log(name + " takes " + damage + " damage.");
if (health <= 0)
{
Destroy(gameObject);
Debug.Log(name + " destroyed!");
}
}
}
Deploying Troops from the UI
Next, we'll set up a simple UI button to deploy troops. In the Canvas, create a button and assign the deployment function.
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
public TroopManager troopManager;
public Button deployButton;
void Start()
{
deployButton.onClick.AddListener(OnDeployButtonClicked);
}
private void OnDeployButtonClicked()
{
Vector3 deployPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
deployPosition.z = 0; // Set z to 0 for 2D
troopManager.DeployTroop(deployPosition);
}
}
Adding Enemy Defense
To make the game more interactive, let’s implement enemy defenses that will attack the troops.
public class EnemyDefense : MonoBehaviour
{
public float attackRange = 2f;
public int damage = 5;
private Troop target;
void Update()
{
if (target != null)
{
if (Vector2.Distance(transform.position, target.transform.position) < attackRange)
{
Attack();
}
}
}
public void SetTarget(Troop newTarget)
{
target = newTarget;
}
private void Attack()
{
// Logic to damage the target troop
Debug.Log("Attacking troop " + target.name);
target.TakeDamage(damage);
}
}
Conclusion
In this tutorial, we have implemented basic troop mechanics for attacking and defending in our Clash of Clans-like game. We covered troop deployment, movement, attacking behavior, and enemy defenses. You can expand these mechanics further by adding troop types, special abilities, and more complex enemy AI.