LSCS: Unit and Formation

In this chapter we will build the GameObjects that will be spawned in your scene. The system makes a clear distinction between the logic level (a unit object with NavMeshAgent) and the display level (a child object with the 3D model and the animator).


1. Unit

The unit prefab is the template for every single soldier on the battlefield.

Purpose: Actual Unit on the Battlefield.
Application: Create a Unit Prefab for every Unit Type you want to have.

Structure of the prefab:
Create an empty GameObject, e.g. „RomanLegionary_Unit“. This is the main object. It requires the following components:

  • Unit.cs: The central „brain“ of the soldier. It manages the status (Idle, Moving, Fighting), the health points and receives orders from its formation.
  • NavMeshAgent: Used for all wayfinding on the battlefield.
  • CapsuleCollider: Used for physical representation and selection by mouse clicks.

Layer: The Unit needs to be assgined to the layer „Unit“.

Child objects of the unit prefab:

  • Visuals: A child object that contains the actual 3D model.
    • Animator: The animator component must be located on this visuals object.
    • AnimatorLink.cs: This script must be on the same object as the Animator. It serves as a high-performance bridge between the unit script and the animator to set parameters. Note that there is a different AnimatorLink for Cavalry Units.
    • RelayAnimationEvents.cs: Must also be located on the animator object. It intercepts events from your animation clips (e.g. the moment in the swing when the attack should do damage) and forwards them to the Unit.cs script.
  • MeleeWeapon / RangedWeapon: GameObjects for the weapons. The unit script activates/deactivates them depending on their status and use. If your Unit is strictly for melee you can leave those empty.
    • RangedWeapon.cs: If the unit has a ranged weapon, this script is applied to the ranged weapon object. It manages the projectiles (Projectile.cs) and the firing point.
      • Weapon: The Ranged Weapons gameObject
      • Animator: The Ranged Weapons animator. Trigger „AttackRanged“ gets called on shot. Can be left empty
      • ShootPoint: Physical shooting Points, should be parent of Projectiles
      • RangedWeapon: ScriptableObject, actual performer of shooting logic
    • Projectile: Actual Projectile. Needs to be a child of the Unit (preferably of the ShootPoint), every projectile needs to be its own gameobejct, no instantiating is used due to performance. Projectile gameobejct should be deaktivated
      • Damage Collider: Trigger Collider for enemy detection, needs to be disabled
      • RigidBody: Standard RigidBody
    • ShieldCollider: Collider of the shield if your unit uses a shield. Used for arrow blocking. Collider needs to be of type trigger and has to be disabled. The Shield neets to have the tag „Shield“ and the layer „Collider“
  • Waypoints: Display objects and actual target of the unit. Prefab can be found under TopsonGames -> Large Scale Combat System -> Prefabs -> Indicators and Waypoint. Drag and dropt it as a child of the Unit Gameobejct and assign the gameobjects.
    • Position Indicator: Displays current position of unit (on hover, select…)
    • Waypoint: Actual waypoint of the unit
    • Waypoint Indicator: Displays new formmation settings (changing of width, position..)
    • Waypoint Marker: Visualization of Waypoints
  • DisableOnDeath: Scripts that should be disabled on death
It is important that the visuals (animator, skinned mesh renderer) are a child of the Unit.cs gameobject

Archer Unit
Melee Unit with Shield

Animation Events

In Order for your Unit to actually attack its enemy it needs to havean animation event at your desired timing:

OnAttack(int 0=Melee 1= Ranged)

It is very important that you call this animation event otherwise no damage will be done. As an integer insert 0 if it is a melee attack and 1 if it is a ranged attack.

Also for switching between Ranged and Melee attack it is neccessary to call

OnEnableMeleeWeapon()
OnEnableRangedWeapon()

at the wished timing.

If your Unit uses a shield to block projectiles it is important to add

OnEnableShield
OnDisableShield

Make sure your unit has the tag and layer Unit

Once all the values are set create a prefab with your unit.


2. Formation

The formation prefab is the „container“ or captain that combines a group of unit prefabs into a single, commandable unit.

Purpose: Defines a complete Formation and its Units.
Application: Create a Formation Prefab for every Formation / Unit Type you want to have.

Structure of the prefab:
Create an empty GameObject, e.g. „RomanLegionary_Formation“. This is the main object.

Formation.cs: The brain of the formation. It manages all unit children, calculates the grid positions, receives commands from the FormationController (player) or AICommander (AI) and manages the state of the entire group (Idle, Moving, Engaged).

Rigidbody: Important! For the OnTrigger events of the child collider to work reliably, the parent object needs a Rigidbody. Set it to Is Kinematic so that it is not influenced by physics.

  • TeamID: TeamID to check if the Formation belongs to the player. Only Formations with the same TeamID as definded in the FormationController can be commanded. When using spawning via the GameManager this value will be set automatically.
  • UnitData: The Previously created UnitData Container
  • Visualizers: Used for Visualizing AttackRange, Detection, Path… Prefabs can be found under TopsonGames -> Large Scale Combat System -> Visualizers. Drag and dropt your prefered Visualizers as a child of your formation and assign them to the list.
    • Archer Range Visualizer: Visualizes the Archer Detection trapezodial set in the ArcherCombatBehaviour
    • Formation Path Visualizer: Visualizes the Path the Formation will take to its new position
    • Melee Detection Visualizer: Visualizes the Enemy Detection of the Formation
  • FormationCollider: Collider that detects Enemy Formations and Projectiles. Needs to have a BoxCollider set to Trigger and a Rigidbody set to kinematic as well as the FormationCollider script. Collider and FormationCollider Mask needs to be set to the layer „Collider“. Prefab can be found under TopsonGames -> Large Scale Combat System -> Formation Collider. Drag and Dropt it as a child and assign the script. Can be used as Flag parent as its position and rotation is set to the center of the Formation

Next drag and drop your previously created Unit prefab as a child of your Formation gameobject and copy it as many times as you want units using STRG+D. Afterwards double click the Create Formation Button to create your formation. The Units List is now automatically filled and your formation is set up wit the spacing and width set in the UnitData Object.

Now that your formation is setup you can create a Prefab out of it.