Combat unit (Mage)
Steps how to create a combat unit, in our case a range unit (mage) which fires projectiles.
Last updated
Steps how to create a combat unit, in our case a range unit (mage) which fires projectiles.
Last updated
To create new combat unit we must again first create a AUnitSO
data set with capabilities, before we create a prefab.
Select Unit type of asset
Click Create New button
Set desired name and description of the new unit
Set cost of production like the new crystal resource itself, or food, wood, etc.
Set production attributes like Population Consumption if applicable for your project; it is used in Demo scene.
Set requirements research or other producible if applicable, like the new Crystal Age research.
Select Capabilities tab
Add Health capability so that unit may be destroyed by enemy units.
Add Attack capability to add support for attacking other entities in scene.
After data asset is created, we can proceed with creating a Unit prefab:
Click on the Create Prefab button if you wish for prefab to be created automatically.
Now click Open Prefab to and open Scene window to see it. You should see 3 components already present (Unit, Health and Unit Attack).
Configure visuals as desired, by default a primitive cube model is present.
Add collider to the root game object, primarily for selection and projectile collisions.
Add movement components. This is required if the unit is to support movement with provided tasks and behaviours (collection, combat, building, etc.).
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
)
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.
Set Layer of the root object (the one with collider) which is used for selection and projectile collision.
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.
Set Projectile Prefab reference, you can use provided projectiles, or create your own by following the steps below.
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.
Create new game object and attach BasicProjectile
script or duplicate existing projectile examples.
Configure script to your project's requirements (followTarget, lifeDuration, etc), the most important on being:
Type - which defines behaviour of projectile's movement or provide your own MovementObject
reference to replace built-in movement
Target Layer - defines collision layer for detecting when target unit has been hit
Ignore Obstacles & Obstacle Layer - when ignoring obstacles is set to false make sure to also set the layer's used for obstacles
Now back to Mage prefab and set the new projectile to the field ProjectilePrefab on the ProjectileLauncher
script.
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 .