Project 1: Space Shooter

Adding a smarter enemy

This one can avoid being shot

Showing the enemies avoiding shots.
The enemies avoiding shots.

To gain an even more diverse enemy base I add a new one. This will detect the player’s laser shot and try to avoid it.

The creation starts the same as the previous ones by adding a new child object to the enemy. Adding RigidBody2D to it, this time the detection zone is done with CircleCollider2D, and then the Detector script.

Could I use the same detection zones as previously? Yes, but doing it this way I can have more variety and control over the zones when all these different zones are separate child objects of the enemy.

Also, if I want to make it, I could produce enemies with random attributes by enabling and disabling child objects with the SetActive method. For now my intention is to make a set of enemies with all of them having predefined features, so that I have more control on how hard or easy I want to make the game by spawning either harder or easier enemy types more.

Anyway, back to the topic. The need for avoiding the laser shot is tracked with the OnTriggerEnter2D and OnTriggerExit2D. When the laser shot enters the detection zone evasive action is taken, and once it exits the normal movement is continued.

Showing the additions to the Detector script.
The additions to the Detector script.

The evasion is triggered with the public method AvoidShot in the Enemy script, once called it changes the Boolean variable _avoidShot either to or , depending on what it was when the method was called. When it is the normal movement is overridden, when false the normal movement is used.

Since the normal movement can be from any direction between the left and right side of the upper part of the game area, the evasive action has to be considered differently for all the cases. The enemies coming from the right try to evade towards the negative x-axis values, and the ones coming from the left try to evade towards positive x-axis values.

Those that move from top to bottom can either evade towards the left or the right, this direction is chosen randomly. Once the evasion direction has been determined, the current enemy’s position is added to the evasion direction and saved to the Vector3 variable _evadeDirection.

Showing the AvoidShot method inside the Enemy script.
The AvoidShot method inside the Enemy script.

This variable is then used in the Movement method where the normal movement is overridden with the _avoidShot variable. There the evasion is achieved with the MoveTowards method. So that the enemy could successfully avoid the shot, the normal speed is multiplied by a speed boost coefficient of 1.5.

Showing the updated Movement method for the enemy.
The updated Movement method for the enemy.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store