The next stage of development of my game was to add more types of drone. The first was a sniper type drone which shoots high damage, long-range shots but is slow to reload and does not have much health. I have also designed the drone to run away from attacking drones.
Skirmish Drone
To get the drone to flee from enemy drones that are attacking, I have added another GameObject as a child to the skirmish drone so I can add another circle collider to it (you can’t add multiple colliders to the same object). I have then created a script so that when an enemy enters the radius of the circle collider, it triggers a response to flee in the movement script.
Circle Collider Radius
After the enemy has exited the radius and the drone has fled from him, the drone can then stop to shoot the enemy. The drone will target this enemy as it is the most threatening.
To flee from the enemy, the flee method calls TurnAway which turns the drone away from the targeted drone (which will be the one targeted by the collider). Below is a code snippet of the TurnAway method which turns the drone away from the target based on the speed of the drone.
private void TurnAway(GameObject newTarget)
{
Vector3 targetDirection = newTarget.transform.position - transform.position;
float step = speed * Time.deltaTime * 10;
float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * -Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, step);
}
For attacking, a very similar method is used, but reversed so the drone faces towards the target.
Commando Drone
Another type of drone that I have come up with is the commando drone which is another ranged drone, but the opposite of the skirmisher as it has a high rate of fire, but low range and low damage. I have used similar principles of the previous drones where the drone has a child that holds a trigger collider, but this one is for working out the range of the drone. When an enemy drone enters the collider, the drone stops and attacks that drone, and if it leaves, it chases.
To shoot, I have a public variable called fire rate which can be modified from Unity. This determines the speed at which the drone shoots. The following is used to see if the drone can shoot and calculates when it should shoot next.
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
...
Both the commando drone and the skirmish drone shoot the same bullet. This bullet has its own script that destroys itself when it collides with anything so the game area isn’t cluttered with old bullets. The script also holds methods to set the damage of that bullet, so when the bullet is instantiated in the skirmish/commando script, the damage can be set. This allows the different drones to have different damages. When the bullet hits a drone, the bullet calls the health component of the drone to take whatever damage it is set to.