---
description: >-
  Steps how to create a combat unit, in our case a range unit (mage) which fires
  projectiles.
---

# Combat unit (Mage)

## Step 1 (Asset)

To create **new combat unit** we must again first create a `AUnitSO` data set with **capabilities**, before we create a prefab.

1. Select **Unit** type of asset
2. Click **Create New** button
3. Set desired **name** and **description** of the new unit
4. Set **cost** of production like the new crystal resource itself, or food, wood, etc.
5. Set **production attributes** like **Population Consumption** if applicable for your project; it is used in Demo scene.
6. Set **requirements** research or other producible if applicable, like the new **Crystal Age** research.
7. Select **Capabilities** tab
   1. Add **Health capability** so that unit may be destroyed by enemy units.
   2. Add **Attack capability** to add support for attacking other entities in scene.



<figure><img src="https://3582497942-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy2G6fDLEPuCAcuABMl6j%2Fuploads%2FsglIrkggVZHqK7J35WKS%2FCreate%20mage%20asset.gif?alt=media&#x26;token=635b186c-7387-4a59-95d0-c083941a4d53" alt=""><figcaption><p>Create Mage unit 1/2</p></figcaption></figure>



## Step 2 (Prefab)

After data asset is created, we can proceed with creating a Unit prefab:

1. Click on the **Create Prefab** button if you wish for prefab to be created automatically.
2. Now click **Open Prefab** to and open **Scene** window to see it. You should see 3 components already present (**Unit**, **Health** and **Unit Attack**).
3. **Configure visuals** as desired, by default a primitive cube model is present.
4. **Add collider** to the **root game object**, primarily for selection and projectile collisions.
5. Add **movement components**. This is required if the unit is to support movement with provided tasks and behaviours (collection, combat, building, etc.).
   1. If you are using Unity's NavMesh, you can attach `NavMeshAgent` for navigating the object and provided `NavMeshMovement` components. (If you are integrating it with Formation asset, use `FormationNavMeshMovement`)
   2. If you are using different navigation system or want to customise your movement component, create a new `MonoBehaviour` script and implement `IUnitMovement` interface. Alternatively you subclass the `AUnitMovement` provided in the package, which has some basic implementation and configuration present.
6. **Set Layer** of the root object (the one with collider) which is used for selection and projectile collision.
7. If your **combat unit** is a **ranged unit**, you can attach `ProjectileLauncher` component which handles shooting projectiles for you. This is achieved by setting the `UnitAttack` component `ManuallyTriggerAttack` flag to true and add listener to the `OnAttack` event on the component.
   1. Set **Projectile Prefab** reference, you can use provided projectiles, or create your own by following the steps below.&#x20;

<figure><img src="https://3582497942-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy2G6fDLEPuCAcuABMl6j%2Fuploads%2FNDYMp1A80QHO3ybsnd6d%2Fcreate%20mage%20short.gif?alt=media&#x26;token=68fec1fb-729d-433d-ba57-10b651bd003c" alt=""><figcaption><p>Create Mage unit 2/2</p></figcaption></figure>



## Step 3 (Projectile Prefab)

In case you'd like to create your own projectile instead of using demo prefabs, here are few things you need to know.

To can create new projectile for mage, you can either use provided `BasicProjectile` script or create your own script and implement interface `IProjectile` on it.

`IProjectile` script must be attached to the root game object of the prefab.

1. Create new game object and attach `BasicProjectile` script or duplicate existing projectile examples.&#x20;
2. Configure script to your project's requirements (followTarget, lifeDuration, etc), the most important on being:
   1. **Type** - which defines behaviour of projectile's movement or provide your own `MovementObject` reference to replace built-in movement
   2. **Target Layer** - defines collision layer for detecting when target unit has been hit
   3. **Ignore Obstacles & Obstacle Layer** - when ignoring obstacles is set to **false** make sure to also set the layer's used for obstacles
3. Now back to **Mage prefab** and set the new projectile to the field **ProjectilePrefab** on the `ProjectileLauncher` script.

<figure><img src="https://3582497942-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy2G6fDLEPuCAcuABMl6j%2Fuploads%2FlWAO36NJ83h7U4hpmp8X%2FCreate%20projectile%20export%20cropped.gif?alt=media&#x26;token=678bdfff-267c-44ce-8100-a1dc8cdb9f3a" alt=""><figcaption><p>Create new projectile</p></figcaption></figure>



## Step 4 (Add to Production)

Now that you have a new production object ready, you must add it to any unit which supports Production itself. You can add this to any existing production structures present in Demo assets, or create your own following the guide for [Mage Tower](production-unit-mage-tower).
