Revisado con versión: 2017.2
Now that we have a tower, we will need something for the tower to defend against. This next section will explain how to create enemy Agents that will try to attack the player’s home base. A large part of the process of getting them to navigate around a stage will be covered in Setting Up a Stage, but we will not be able to do that without first setting up an Agent GameObject with the necessary components.
Note that we will be using the term ‘Agent’ instead of ‘Enemy’ to refer to these autonomous actors.
Creating an Agent
The first step is to make an Agent Configuration.
- In the Project window, click on the Create menu at the top right and choose: Create > Tower Defense > Agent Configuration
We now have an Agent Configuration, where we can set values for the agent.
Next, we must create the Agent GameObject.
Create an empty GameObject
Name the GameObject NewAgent
Add a HitscanLauncher or BallisticLauncher component to the game object. Refer to https://unity3d.com/learn/tutorials/puroziekutos/tower-defense-template/targeting-and-firing for more information
Add an AttackingAgent or FlyingAgent component to the GameObject
Drag the Enemy SimpleAlignment ScriptableObject into the Alignment field
Add a mesh to visualise the Agent as a child object of the Agent GameObject
Create an empty child GameObject of the Agent and name it Targetable. This object will ensure that Towers view the Agent as a valid target of attack
Select the Agent in the Hierarchy window and drag the Targetable into the Target Transform field of the Agent component
Note that a NavMeshAgent component is added automatically upon adding an AttackingAgent or FlyingAgent component. The NavMeshAgent component is required to ensure the Agent can move around the level. The values defined within it specify how it does that.
As is the case with towers, an Agent cannot be defined simply by attaching a AttackingAgent or FlyingAgent component to a GameObject. Some other components will be required to ensure that the Agent carries out its required behaviours.
- Add an appropriate Collider component that encapsulates the Agent’s mesh to the NewAgent GameObject
A Collider is necessary for an Agent to be able to determine whether or not it was hit by a projectile.
- Add a DamageCollider component to the NewAgent GameObject
Understanding the AgentConfiguration
This is the name of the Agent.
A description string to describe the Agent and its capabilities.
The Prefab that contains the AttackingAgent or FlyingAgent component for this Agent.
Understanding the AttackingAgent and FlyingAgent components
The Starter Kit includes two different Agent components: AttackingAgent and FlyingAgent. These two agents respond differently if their path is blocked.
AttackingAgent will stop and attack nearby towers until a path becomes available and depends on an AttackAffector to operate (see Targeting and Firing below). FlyingAgent will simply fly over obstacles directly to its destination, ignoring usual NavMesh behaviour.
Both of these components extend the Agent base class, and are configured in exactly the same way. We can also implement our own logic for how agents behave by creating scripts that extend the Agent base class.
The maximum health of this Agent.
The starting health of this Agent.
Alignment defines which "team" the Agent is on, and therefore what the Agent targets and is targeted by. When creating enemy Agents, this will usually be set to Enemy.
Ordinarily when Towers or Agents target each other, they will aim for the origin of their transforms. The TargetTransform allows us to set an alternate point to fire at.
Applied Effect Offset
If a Tower applies an effect to the Agent, this value allows us to adjust the position of that particle effect.
Applied Effect Scale
If a Tower applies an effect to the Agent, this value allows us to adjust the size of that particle effect.