Aggressive Enemy Type
Objective: Learn how to implement enemy aggression on a new enemy type.
So we want our enemies to be tougher, right ? Well, first lets take a look at our current enemies. We only have 2.
Default: Ship does downwards and sometimes move sideways. Every couple of seconds it fires a laser downwards.
Zigzag: Ship moves in a zigzag pattern, meaning its always going down diagonally and changes directions very fast. Don’t fire lasers.
So we are going to create a new enemy, he won’t move sideways, but he will be faster. If he spots the player near him, he will run towards the player for its inevitable death.

Lets now create our new enemy using the sprite shown above.

So we have our new enemy created with its turbo properly in place, now we have to make sure to create the animation, and give our enemy a rigidbody2d and collider components.
Now lets update our Enemy Behavior script, to make sure we have the option to select our new enemy.
private enum EnemyType
{
Default,
Zigzag,
Aggressive
}
Now we just make sure to update our enemy script in the inspector with all proper information.

Before we move to our Enemy, lets first make sure that our SpawnManager has access to our new enemy, and we added the chance for it on our spawn table.

int[] enemyTable =
{
60, // Default Enemy
40, // Zigzag Enemy
20 // Aggressive Enemy
};
Now our SpawnManager is able to spawn our new enemy, with a chance of 20/120.
Now we are going to make sure our Aggressive Enemy attacks more frequently than our Default one.
private void FireLaser()
{
if(_type == EnemyType.Aggressive)
_fireRate = Random.Range(1.5f, 3f);
else
_fireRate = Random.Range(3f, 7f);
_canFire = Time.time + _fireRate;
Instantiate(_laserPrefab, transform.position, Quaternion.identity);
}
Now we need to make sure to factor where the player is before we start to move towards it, lets make a method that checks it for us.
private bool IsPlayerNearby()
{
float detectionRadius = 3f;
LayerMask playerLayer = LayerMask.GetMask("Player");
if(Physics2D.OverlapCircle(transform.position, detectionRadius, playerLayer) != null)
return true;
return false;
}
So first we created a Layer for our player, and attached to our Player object.

Now we create a radius for our circle, which is going to be 3f, then we just assign our layer to a variable.
Inside our if statement, what we are doing is creating a circle, around the position of our player, with the size of 3f (which we determined), trying to detect the playerLayer. This will either result true or false, in case its true, that means we found our player object and should move towards it, so we return true, otherwise we just return false.
private void CalculateMovement()
{
switch (_type)
{
//Other cases
if (IsPlayerNearby())
{
Vector3 playerPosition = _player.transform.position;
transform.position = Vector3.MoveTowards(transform.position, playerPosition, _enemySpeed * Time.deltaTime);
}
transform.Translate(_enemySpeed * Time.deltaTime * Vector3.down);
break;
}
CheckForBounce();
TeleportNewPosition();
}
When checking for our movement, first thing we do is call our IsPlayerNearby method. This will return true or false. If we return true, that means our player is within our radius, so we want to move towards him.
We grab the player position, and us Vector3.MoveTowards, passing our current position and the player’s position.
Regardless of what we returned in the method, we always move downwards anyways.

Now as we can see in the example, we can have an aggressive enemy, which hunts the player in order to collide, and worse of all, he sometimes has a shield!