Aggressive Enemies

Post MVP Feature Add # 13

Eric Philippot
2 min readAug 10, 2022

This is actually really fun to make. I have been putting a lot of time in getting the game balance right. So what we have here is an enemy that will aggressively attack the player when it’s health is below 40 points and within a specified distance to the player. This provides a really fun way to play because you can’t tell what enemy is low on health till it charges.

One of the things that I like a lot and definitely want to dive deeper into is AI. The challenge of making well balanced bots is very interesting.

Here is how we implemented the enemy aggression in code.

Enemy script with the logic to make the enemy attack the player when it’s health is below 40 points.
Enemy Script — Aggressive Attack

So above we have it set that if the enemy is less or equal to 40 points of it’s health, it will charge at the player IF it is withing 6 meters of it. It the ChargePlayer() method we have it set to check the distance

distance = Vector3.Distance(this.gameObject.transform.position, _player.gameObject.transform.position);

and if the distance is below what we set it to, we move the enemies position toward the player’s “x” position, while still keeping its normal “y” movement.

if (distance < attackRadius){this.transform.position = Vector3.MoveTowards(this.transform.position, 
new Vector3(_player.transform.position.x,
this.transform.position.y - 10f, 0f),
_curve.Evaluate(_speed + 10) * Time.deltaTime);
}

I set a -10 to the “y” position because when I didn’t have that there, the enemy sort of just sat there and followed the players “x”. Which could be a good mechanic, but not for this game.

I then used the AnimationCurve again because I liked how well it worked with the power up movement. If the enemy was within the specified radius of the player, it continued it’s normal path.

else{transform.Translate( (new Vector3(_directionChange, -1f, 0f) * _speed) * Time.deltaTime);}

I still have some tweaking and code refactoring. But I like how this is coming a long and testing and making new mistakes has helped with ideas on how to add new features and maybe making this into a full game.

--

--

Eric Philippot

Game and Web Developer with a joined experience of 6 years.