Unity RTS Engine

  1. Home
  2. Unity RTS Engine
  3. Attack Behavior
  4. Attack Component

Attack Component

Add the Attack component to the unit/building main object in order to allow it to attack enemy units/buildings.

General Attack Settings:

  1. Attacker: The attacker type, either a unit or a building.
  2. Attack Code: Unique code for this attack.
  3. Attack Power: Holds a value that describes how strong the attack is. This value is used by NPC factions to build an army.
  4. Attack Icon: Icon that represents the attack (this is only used when the attacker is a unit and it has more than one attack type, the icon will be used in the task panel allowing the player to switch attack types).
  5. Basic Attack: When there are more than one attack type per unit then one of them should be selected as the basic attack type, the basic attack type is the one that is activated for the unit per default.
  6. Attack Reload: Time (in seconds) between two consecutive attacks.
  7. Attack All Types: When enabled, then the attacker will be able to attack all unit/building types without exceptions. When disabled, the following settings will appear:

    1. Attack Units: Disable if you don’t want the attacker to attack units.
    2. Attack Buildings: Disable if you don’t want the attacker to attack buildings.
    3. Attack Exceptions (Codes): Unit/building codes that represents units and buildings that the attacker can’t attack.
    4. Attack Only In (below) List / Codes List: Here you have the option to either define a list of units/buildings (by entering their codes in the Codes List array) and make the attacker only able to attack them (when “Attack Only In List” is enabled) or make the attacker attack all units/buildings at the sole exceptions of the ones in the list (when “Attack Only In List” is disabled).
  8. Range Type Code (units): Input the code of the range type that the unit belongs to (Range Types are explained in the Attack Manager documentation page).

Attack In Range Settings:

  1. Attack In Range: Enable if you’d like the unit to search for targets inside a defined range and attack them. When enabled, two options will be displayed.
    1. Search Range: This is the range where the unit is allowed to search for targets.
    2. Search Reload: Time between each search for targets to attack.

Attack Unit Settings:

The following settings will only appear if the attacker type is set to “Unit”.

  1. Attack On Assign: Enable if you’d like the player to pick and assign targets (with the right-mouse button) for the unit to attack.
  2. Attack When Attacked: Enable if you’d like the unit to attack back when a unit/building attacks it (if it doesn’t has an attack target already).
  3. Attack Once: Enable if you’d like the unit to only attack once and then the attack target has to be assigned again.
  4. Move On Attack: Enable if you’d like the unit to launch attacks while moving.
  5. Follow Distance: If the target decides to run away, this value represents how far the unit will follow the target. If the target’s distance exceeds the value in this field, then the attack will be cancelled.
  6. Play Animation In Delay: Enable if you’d like to play the attack animation as soon as the delay timer starts (not the actual attack). Delay is further explained below.
  7. Attack Animation Duration: Input the duration of the unit’s attack animation in this field.

Attack Damage Settings:

  1. Deal Damage: Enabled if you’d like the attacker to deal damage to its target (when this is disabled, attack-related custom delegate events will still be called so you can choose what happens instead of dealing damage).
  2. Area Damage: Enabled if you’d like the attacker to cause damage that covers a whole area and not a single unit/building. When this option is enabled, the following options will appear: The Attack Ranges array allows you to define areas where different damage points will be dealt to units/building depending on their distance from the original attack position. Each element of the array represents an area, areas must be defined from the closest areas to the attack source to the furthest.

    1. Range: Size of the area where the damage will be applied.
    2. Unit Damage: The damage that will be dealt to units in this area.
    3. Building Damage: The damage that will be dealt to buildings in this area.
  3. Unit/Building Damage: If the Area Damage option is disabled, the two fields “Building Damage” and “Unit Damage” will appear where you can define how many damage points this attack deals to units/buildings.
  4. Damage Over Time (DoT): It’s also possible to apply damage over time. The damage points defined in the above two fields (whether it’s area damage or not) will be applied over time. When this option is enabled, the following fields will appear:

    1. Infinite DoT: If enabled, the DoT will be applied to the attack target till the target is dead/destroyed or you the DoT is stopped with your custom code.
    2. DoT Duration: If the DoT is not infinite, then you need to input how much it will lasts in this field (in seconds).
    3. DoT Cycle: The time period (in seconds) at which the damage will be applied to DoT victim (if DoT Cycle is set to 4, then every 4 seconds the damage will be applied).
  5. Reload Dealt Damage: All damage points applied from the attacker are accumulated and saved in the float type attribute “DamageDealt” of the Attack class. This allows you to reward the attacker when he deals X amount of damage. This value can be reloaded each time if this option is enabled (each time the target acquires a new attack target, the value of “DamageDealt” will reset).

Attack Delay/Cooldown:

  1. Attack Delay Time: Before the attack is launched, a delay time can be assigned that delays the attack (this especially useful when you want to start the attack animation for a unit before the actual attack is launched so the trick here is to set the attack delay time to the value at which the unit attack animations progresses enough to launch the attack, then enabled the “Play Animation In Delay” option.
  2. Use Delay Trigger: When enabled, then in addition to the attack delay time, it is required that you access the “AttackTriggered” bool attribute in the unit’s Attack component and set it to true in order to trigger the attack. This allows for more customization for the Attack component.
  3. Use Cooldown: When enabled, the unit won’t be able to launch an attack when in cooldown mode.
  4. Cooldown duration: Duration of the cooldown if it’s enabled.

Direct Attack/Attack Object:

  1. Direct Attack: When enabled then the damage will be directly applied to the attack target when the attack is launched. When disabled, the attacker will create an Attack Object and launch it towards the target, the damage is then applied only if the Attack Object hits the target.
  2. Indirect Attack/Attack Objects: When Direct Attack is disabled, then the Attack Sources array will appear in the inspector and right above it is the Attack Type field: When using attack objects, there can be two types of attacks: either Random or In Order. Since it is allowed to have multiple attack sources (multiple attack objects as well), only one attack source will be chosen and the attack object will be released from there if the attack type is random. If the attack type is in order then all the attack sources will launch the attack objects in the same order that they are added in the Attack Sources array. The In Order mode allows the attacker to have a more complex attack where for example a robot with multiple machine guns attached to it can attack with bullets coming out from all machine guns at the same time or with a delay between each shot. Attack sources, objects are managed in the Attack Sources array:
    1. Delay Time: This represents the time that will be waited before the attack is created/launched.
    2. Create On Delay: When enabled, the attack object (defined below) will be created even if it’s in delay mode but it won’t start moving until the delay is over.
    3. Damage In Delay: When this is and the above option are both enabled then the attack object will still give damage to enemy units/buildings even in delay.
    4. Delay Parent: You can assign a parent transform for the attack object when it’s still in delay (and while having “Create On Delay” enabled).
    5. Attack Delay: Time (in seconds) before the attack is launched from this source.
    6. Attack Obj: The attack object prefab. This object must have both the EffectObj and AttackObject as components. For the EffectObj component, please check this documentation page for an explanation of its fields in the inspector. As for the AttackObject component, it only has one field: Spawn Effect. The SpawnEffect is also an effect object spawned as soon as the attack object is created, it can be a muzzle flash for a bullet for example. Please check this documentation page for more information about effect objects. The attack object also includes a collider that has “Is Trigger” set to false and a rigidbody component with fixed rotation and position on all axis, in addition to having “Is Kinematic” set to false and “Use Gravity” set to false.
    7. Attack Obj Destroy Time: Life duration of the attack object before it gets destroyed.
    8. Attack Obj Source: Where should the attack object spawn at? It should be either the main unit/building transform or a transform that belongs to a child object of the unit/building prefab.
    9. Attack Obj Speed: Speed of the attack object when it moves towards the target.
    10. Damage Once: When enabled then the attack object will only do damage to the first enemy unit/building it hits. 
    11. Destroy On Damage: When enabled, the attack object will be destroyed as soon as it hits an enemy unit/building.
  3. Attack Effect: This object is spawned at the target’s position whenever damage has been dealt either with a direct attack or using an attack object. Please check this documentation page for more information about effect objects and the Effect Obj component.
  4. Attack Effect Duration: If an Attack Effect is assigned, then this field represents the life duration of this effect object before it gets hidden.

Attack Weapon:

  1. Weapon Object (optional): It is possible to have a weapon object that acts as the parent object of the attack source. Assigning that weapon object to this field will allow the unit/building to rotate it to face the attack target making the whole attack process more realistic. All the following fields in this part should be only considered when this field is not empty.
  2. Freeze Rotation on X, Y and/or Z: When rotating the weapon object, you can pick to block rotation on one or a combination of the three axis.
  3. Smooth Rotation: When enabled then the rotation of the weapon object to face the attack target will be done smoothly. When disabled, the weapon object will directly rotate to face the target.
  4. Rotation Damping: Determines how smooth the weapon object rotation is, if the above option is enabled.
  5. Force Idle Rotation: Enable if you’d like the attacker’s weapon to get back into an idle rotation when the attacker is idle (not attacking).
  6. Weapon Idle Angles: If the “Force Idle Rotation” is enabled and the attacker is in idle mode (does not have a target), then the weapon object will take this rotation (the rotation is defined here by Euler Angles).

Line Of Sight (LOS):

  1. Enable LOS: This enables the Line Of Sight (LOS) feature for this attack type. When enabled LOS will only allow the attacker to launch an attack if the target is in a defined range of the attacker’s sight.
  2. Use Weapon In Los: When enabled, then the LOS calculations will be made in reference to the weapon object’s rotation (which can be assigned in the above fields). If the weapon object isn’t assigned or this is disabled then all LOS calculations will be made in reference to the main attacker’s transform rotation.
  3. Allowed LOS Value: The lower this value, the stricter is the LOS requirement is. You can increase/decrease this value till you get the behavior you want from the attacker.
  4. Ignore the X, Y and/or Z axis while checking for line of sight.

Attack Audio Settings:

  1. Attack Order Sound: An audio clip played when the player gives an order for this attacker to launch an attack.
  2. Attack Sound: An audio clip played every time the attacker launches an attack.

How can we help?