Targeting and Firing

Vérifié avec version: 2017.2


Difficulté: Intermédiaire


The previous sections provide instructions on how to create Agents and Towers, but don’t cover how to actually have them fire at each other. This is, in large part, because the same components and processes are required for both enemies and towers. We will go through these in the following section.

Creating a Projectile

Before making Agents and Towers capable of shooting, we’ll need to make the projectiles for them to shoot at each other. The amount of damage they’ll do is a property of the projectile, rather than the Tower or Agent itself.

  • Create an empty GameObject and give it a name made up of its Tower/Agent type, the word ‘projectile’, and the level it will be associated with, if appropriate (eg. LaserTowerProjectile_1)

  • Add a Damager component to the new Projectile object

  • Set the Damage field to indicate the amount of Health that should be removed from Towers or Agents the projectile hits

  • If the projectile is for a Tower, drag the Player SimpleAlignment ScriptableObject into the Alignment field. If it is for an Agent, drag the Enemy SimpleAlignment ScriptableObject into the Alignment field

Further steps depend on what kind of projectile is being created. For a hitscan projectile, which doesn’t rely on ballistics or collision detection:

  • Add a HitscanAttack component to the Projectile GameObject.

  • Set the Delay field to a desirable number of seconds.

For a projectile such as a rocket, where we check to see if and where it collides with an object to determine damage:

  • Add a BallisticProjectile component to the Projectile GameObject

  • Add a RigidBody and CapsuleCollider component to the Projectile GameObject to ensure that collisions between the Projectile and the environment can be detected

  • Add a ContactDestroyer component, which will destroy the Projectile after a collision

If the projectile is meant to do damage in a wider radius, take the following steps:

  • Add a SplashDamager component to the Projectile GameObject

  • Set the desired radius for the area of effect in the Attack Range field

  • Set the value of the Damage Amount field to the amount of damage that Agents/Towers not directly hit by the Projectile should take

Adding Targeting to a Tower

The following steps will enable targeting on both Towers and Agents. The Starter Kit uses a separate child GameObject for the AttackAffector on Towers, whereas the component is attached to the Agent Prefab itself. This is purely an organisational difference, and does not change how the AttackAffector functions.

  • Create an empty child GameObject of the Tower level/Agent Prefab and name it Affector. This object will ensure that Towers and Agents can fire at each other
  • Add an appropriate Launcher component to the Affector GameObject
  • Add an AttackAffector component to the Affector GameObject
  • Drag a projectile Prefab into the Projectile field
  • Add a Projectile Point as a location from which the Tower/Agent will fire the projectile
  • Set the rate of fire for the Tower level/Agent
  • Create an empty child GameObject of the Tower level/Agent Prefab and name it 'Targetter'. Apply a Targetter Component to the GameObject. This object will ensure that the Tower is able to find Agents to fire at
  • Drag the Tower level’s turret Transform into the Turret field. The object set as the turret will point towards Agents
  • Set the Tower level’s search rate
  • Set the Tower level’s collider to be a sphere or capsule
  • Set the Radius and Vertical Range fields

  • Select the Affector GameObject in the Hierarchy window and drag the Targetter GameObject into the Tower Targetter field in the AttackAffector component

  • Apply changes to the Tower prefab

The above steps will ensure that an Agent can fire at Towers that block its path to the player’s home base. In order to make the Agent damage the home base as well, take the following steps

  • Add a HomeBaseAttacker to the Agent Prefab

  • Set the number of seconds the Agent should spend charging up before attacking when it reaches the home base

Understanding the Launcher components

There are a number of launchers available in the Tower defense Template.

Ballistic Launcher

The BallisticLauncher calculates the trajectory of the projectile using physics and launches a BallisticProjectile. More information on the BallisticProjectile can be found in the Action Game Framework Reference.

Fire Particle System

The Particle System that should play when the tower fires using this launcher.

Hitscan Launcher

The HitscanLauncher immediately places a HitscanAttack on a target. The HitscanAttack will then (with an optional delay) damage the target.

Fire Particle System

The Particle System that should play when the tower fires using this launcher.

Super Tower Launcher

The Super Tower appears in the example project, it automatically destroys itself after a certain amount of time.

Fire Particle System

The Particle System that should play when the tower fires using this launcher.

Tower Life Span

The number of seconds before the Super Tower self-destructs. This field is included to balance the powerful Super Tower by preventing it from being in play for too long.

Fire Vector X Rotation

FireVectorXRotationAdjustment represents the amount that the SuperTower’s turret rotates based on the direction the SuperTower’s projectiles (see WobblingHomingProjectile in Action Game Framework Reference).


The function the SuperTower uses to destroy itself is exposed to allow for configurable handling of the Super Tower’s self-destruct function. This allows us to prevent the Super Tower from killing itself, for example.

Understanding the AttackAffector component

The AttackAffector component allows a Tower or Agent to fire at each other.


The Prefab that is spawned when this AttackAffector fires at a target. The specific type of Projectile will depend on the Launcher component.

Projectile Points

A list of Transforms. When a Transform is added to this list, projectiles may spawn at the position of that Transform. This is useful for towers or enemies that have multiple turrets.

Is Multi Attack

If set to true, the AttackAffector will be able to find multiple targets if they are in range. If set to false, the Affector will target one enemy at a time until either the enemy is destroyed or the enemy moves out of range.

Fire Rate

This controls how often the AttackAffector will tell the Launcher to launch a projectile.

Random Audio Source

A list of AudioSources. A random one will play each time the AttackAffector fires.

Tower Targetter

A reference to the Prefab that contains the Targetter component for this Tower level.

The Targetter keeps track of the number of targets in range and specifically the closest target to the Targetter’s epicentre. For feedback, it also controls the turret mesh which is a child of the Tower level Prefab and aims it at the nearest target to communicate to the player that it is the closest one.

Radius Effect Color

This changes the color of the range visualizer that shows how far the tower can fire.

Understanding the Targetter component


The turret mesh attached to the prefab for this tower level.

Turret X Rotation Range

Value in degrees that determines how far the Tower can look up and down.

Only Y Turret Rotation

Whether or not the Tower can rotate around the X axis.

Search Rate

The Targetter’s SearchRate configures how often a new closest target is selected.

Idle Rotation Speed

How fast the tower rotates when it has no target to fire at.

Idle Correction Time

The time in seconds the Tower will wait after losing a target before it returns to its neutral position.

Idle Wait Time

The time in seconds the Tower will wait after losing a target before it begins its idle rotation again.

Attached Collider

A reference to the Targetter Collider set below. This is automatically set when using the Targetter Collider field.

Targetter Collider Configuration

The Targetter’s search volume can either be a capsule or a sphere. When the collider is a sphere, the radius can be configured. When the volume is a capsule the vertical range of the capsule can also be configured.

Targetter Collider

Whether the Targetter’s collider is a capsule or sphere.


The Targetter’s firing range in standard Unity units.

Vertical Range

How far above or below itself the Tower is able to fire.

Understanding the HomeBaseAttacker component

Home Base Attack Charge Time

This defines the amount of time (in seconds) between the Agent reaching the final node in the level, and it reducing the health of the player’s home base.