NPC Attack and Defense Handling
1. Prepare Attack Units and Buildings
Following this previous guide, we will create NPC Unit Regulator Data instances for the following units and buildings and then assign them for each entity prefab
1.1 Archer Unit
Create a NPC Unit Regulator Data for the tutorial_archer_level1 unit and attach it to the unit prefab's NPC Unit Regulator Data Input component and match it with the tutorial_npc_type NPC Type:
1.3 Barracks Buildings
Create a NPC Building Regulator Data for the tutorial_barracks unit and attach it to the unit prefab's NPC Building Regulator Data Input component and match it with the tutorial_npc_type NPC Type:
Then add the tutorial_barracks prefab to the NPC Building Creator component of the tutorial_npc_manager like the following screenshot so that the NPC faction can create the barracks building.
2. NPC Attack Manager
Open the tutorial_npc_manager prefab and find the NPC Attack Manager component attached to the NPC.Attack child object. This component is responsible for setting a target faction ID for the NPC faction to attack, launching the attack and actively picking target enemy units and buildings to attack during the attack.
2.1 Picking Target
- Can Attack: Can the NPC faction attack other factions? Disable to deactivate this component's behavior.
- Attack Resources: Resource types whose values determine the criteria of evaluating a potential target faction's strength. Here we assign one resource type and that is the faction_power. The tutorial_archer_level1 unit increase the NPC faction's faction_power resource count when it is created and since it is the only attack unit that we will be using, we can use this as a judgment factor for how strong a faction is.
- Target Faction Type: What's the NPC faction's strategy when picking a target faction?
- Set Target Faction Delay: Delay time before picking a target faction? Options include Random, Most Attack Resources (where the NPC targets the faction with the highest faction_power count in this case) and Least Attack Resources (the one we picked for this example so our NPC faction wil target the faction with the least count for the faction_power resource).
2.2 Launching Attack
- Launch Attack Reload Range: How often does the NPC faction consider whether to launch an attack or not?"
- Launch Attack Resources: Minimum amount of resource types required to launch an attack. In this example, we use the faction_power again so that we make sure that the NPC faction only launches an attack when that resource count reaches a value between 80 and 120, meaning it has created around 6 to 8 instances of the tutorial_archer_level1 units.
2.3 Handling Active Attack
- Attack Order Reload Range: How often does the NPC faction pick the next target unit/building to attack while engaging in an attack towards enemy faction?
- Target Picker: Define the buildings/units that the NPC faction will prioritize targeting during an active attack. In this example, we will target all enemy faction entities equally.
- Target Picker Only: Only target enemy faction entities that are defined in the above Target Picker field?
- Target Preference: NPC faction preferences when picking the next faction entity target using this component. The preference allows you to make the NPC faction prioritize units over buildings or vice-versa.
- Cancel Attack Resource: If the faction has one of the following resource types under the specified amount, the faction will stop its active attack and retrieve its attacking army of units back to its territory. Here we define the faction_power resource type with an amount between 30 and 50 meaning that when the NPC faction is actively pursuing an attack and the instances of the tutorial_archer_level1 unit reach between 3 and 4, then the attack will be cancelled.
3. NPC Defense Manager
In the tutorial_npc_manager prefab, you also find the NPC Defense Manager cormponent attached to the NPC.Attack child object. This component is responsible for monitoring the NPC faction's territory when an enemy faction attacks to alert attack units to defend the NPC factions as well as altering attack units to provide support for each other when engaging with enemy faction entities.
3.1 Territory Defense
- Can Defend Territory: Enable to allow the NPC faction to defend its territory when a unit or a building that it owns is under attack inside its territory. The defenese is simply having the NPC faction's attack units aware of enemy entities inside its territory with the goal of eliminating them.
- Cancel Attack On Territory Defense: Cancel an active attack if the NPC faction's territory is under attack?
- Cancel Territory Defense Reload Range: How often does the NPC faction decide whether it has to keep defending its territory or stop?
3.2 Unit Support
- Unit Support Enabled: Enable to allow a NPC unit to ask for support from units in its range when it is attacked.
- Unit Support Range: If unit support (above field) is enabled, then this is the range in which units can be called for support.
4. NPC Unit Behaviour Manager
Now that we have defined the attack and defense configurations for our NPC faction, we need to make the tutorial_archer_level1, the attack one, react to these configurations. For this, we need to use the NPC Unit Behaviour Manager component which allows units to react to attack and defense orders coming from the two components we configured above.
In the tutorial_npc_manager prefab, find (or add if it is not there) the NPC Unit Behaviour Manager component attached to the NPC.UnitExtension child object and let's configure it for the tutorial_archer_level1. You can have as many instances of this component as you need so you can have different behaviour management configurations for different unit types in the NPC faction.
- Prefabs: Prefabs of unit types whose behaviour would managed by this component. Drag and drop the tutorial_archer_level1 unit here.
- Force Creation: Pick parameters that allow to force the NPC faction to update the creation goals (target amount) of the assigned unit types:
- Enabled: Allow to periodically update the target amount of the faction entity type(s) to force the NPC faction to create more of it? We will have this field disabled so we allow the unit to be created according to its defined regulator data without manipulating the target count.
- Target Count Update Delay (in seconds): Delays updating the target count of the faction entity type(s) after the game starts.
- Target Count Update Period (in seconds): How often to increase the target count of the faction entity type(s)?
- Target Count Update Amount: Amount to update the target count of the faction entity type(s) each period.
- Attack Engage Order Behaviour: Pick parameters that allow to define the behaviour of the handled unit instances when an attack engagement order is set by the NPC faction.
- Send: Enable to send the unit instances to the attack target/destination when one is provided.
- Send Idle Only: Only send unit instances that are in an idle state? We will have this disabled.
- Send No Target Threat Only: Only send unit instances who have an active target that can not attack back?
- Send Ratio Range: Ratio of the unit instances to send over all available to send instances that will be tasked with moving to the attack target/destination. <=0.0f means that no instances will be sent and >=1.0f means all available instances will be sent.
- Send Delay: Delay time before the available and eligible unit instances are sent to the attack target/destination?
- Attack: Enable to launch an actual attack with the unit instances to send. Or disable to only move the units towards the attack target/destination. Here we want this field enabled because we will be using the tutorial_archer_level1 unit to perform attacks.
- Send Back On Attack Cancel: Send back units to their spawn positions when the attack is cancelled?
- Territory Defense Order Behaviour: Pick parameters that allow to define the behaviour of the handled unit instances when the NPC faction announces a territory defense state.
- Defend: Enable to allow the units to be aware of all enemies inside the territory of the building center where the defense order was issued.
- Force Change Defense Center: Enable to force changing the building center whose territory would be defended by the units. The force only applies to units who do not have an active attack target at the time of the defense order.
- Send Back On Defense Cancel: Send back units to their spawn positions when the territory defense mode is cancelled?
5. Testing
5.1 NPC Attack
Play the map scene and allow the NPC faction to build the tutorial_barracks building and then instances of the tutorial_archer_level1 unit and as soon as those unit instances hit a certain count, it will launch an attack by sending them to destroy the local player faction entities:
When we look at the NPC Attack Manager logs, we can see what current faction the NPC faction is targetting, the current counts of the resource types assigned to be the conditions for launching and cancelling the attack as well as the event logs where we see the NPC faction pick a target faction, then launch an attack on the enemy faction as well as picking a specific faction entity to attack.
As for the NPC Unit Behaviour Manager logs, we can see it tracks the tutorial_archer_level1 unit and in the event logs, it responds to the NPC faction attack orders by sending units to attack the assigned enemy target.
5.1 NPC Defense
When we now create attack units and send them to attack the NPC faction, we can see it send some of its created tutorial_archer_level1 units to defend its territory our units reach the NPC faction territory and attack one of its faction entities:
When we look at the NPC Defense Manager logs, we can see the defense orders and unit support orders that the NPC faction is sending as well as what current building center it is focusing its defense on.
As for the NPC Unit Behaviour Manager logs, we can see it react to the defense orders received from the NPC faction and sending units to defend the NPC faction's territory.
Download Files
You can download the results of this tutorial here.