In this lesson, you will create a simple Turret that rotates to face enemies that are nearby.
weapon-ballista
model but you may use any model)TurretTarget
MonoBehaviour ScriptTurretTarget
component to your Turret PrefabIf all went well, your Scene should look similar to this:
Before your turret can rotate to face an enemy, it must be able to detect enemies that are within range. To do this, you will define an Area of Engagement using a collider that will track enemies that are within range.
You can change the range of the Area of Engagement by adjusting the scale
Currently, your Area of Engagement is being drawn as a hideous sphere in both the Scene and Game views.
You can create a transparent Material that will allow your player to visualize the area of engagement while not preventing them from seeing enemies that pass through.
Adjusting the Metallic Map and Smoothness property of the Surface Input changes how reflective the material appears. To make the material have no "shine" to it, set both of these values to 0.
You may have noticed that it is quite difficult to determine which tiles are within the area of engagement in the isometric game view, especially the tiles that are on the far side of the turret.
To help with this, you can modify the Y scale of the Area of Engagement. This will make it more obvious where the edge of the collider is located.
Note: The Sphere Collider uses the largest of the 3 scales to determine its size. The Colliders are shown in the Scene View using green lines. You can verify the size of your Sphere Collider by clicking on the Area of Engagement in the Hierarchy.
Before you can detect if an enemy has entered an Area of Engagement, you must first add a Collider to your Enemy Prefab.
When you're finished, your Enemy Prefab should look similar to the image below:
You can use the Collider.OnTriggerEnter(Collider)
method to detect when a Collider has entered a Trigger Collider. However, for the physics system to detect this, at least one of the Game Objects must be part of a Rigidbody. In this case, you can add a Rigidbody to the Area of Effect that is Kinematic.
At this point, everything is ready for you to detect when an Enemy has entered the Area of Engagement using the OnTriggerEnter(Collider)
method.
AreaOfEngagement
MonoBehaviour Scriptvoid OnTriggerEnter(Collider)
to your AreaOfEngagement
MonoBehaviourDebug.Log()
to verify that the trigger is workingIf all went well, your console should look similar to the video below
You can use a List<Transform>
to track the potential targets.
List<Transform> Targets
property to your Area of Engagement
OnTriggerEnter
, add the entering collider to the Targets
If all went well, you should see the Enemy's collider being added to the Targets list when they enter the trigger collider.
Next, you will want to make it such that your Area of Engagement removes Enemies as they exit the trigger collider.
OnTriggerExit(Collider)
such that the collider's transform is removed when the enemy exits the Area of Engagement.If all went well, you should see the collider's being removed from the Targets list in the inspector. You may find it useful to increase the size of the Area of Engagement to test this.
With your Area of Engagement script tracking the enemies that are near the Turret you can now implement the TurretTarget MonoBehaviour to rotate to face one an Enemy in the Targets list
AreaOfEngagement AreaOfEngagement
propertyGameObject Model
propertyUpdate
, if AreaOfEngagement.Targets
is not empty
Model.transform.LookAt
to rotate the model to face the first Enemy in AoE.TargetsIf all went well, you should see your Turrets rotating to face enemies as they pass by:
With your turrets successfully tracking the enemies as they pass by, it is time to create a Projectile that can be fired at the enemies to destroy them.