In this article, I will cover how I created an enemy that can void player fire. The behavior being creates is the following…
- When the player fires towards the enemy it will either move left or right.
- The left or right movement is picked randomly and stops the movement of the enemy after some time.
- If the enemy moves off the screen it will wrap around to the other side.
Dragged a sprite on to the Canvas, scaled it down to match the other ships in the game. Add a Rigidbody2D, CircleCollider2D and a new script.
I need a way for the enemy to be able to detect when a player laser is close enough to trigger dodge movement. We cannot use the CircleCollider attached to the enemy because we need that to detect when the enemy is actually hit by the player’s lasers. So, I went with creating an empty child to the Enemy and attaching a BoxCollider to this empty game object.
For the empty game object called Shot Detection, we repositioned in front of the enemy and adjusted the BoxCollider to the size I want to the be able to detect the laser coming from the player. For this Shot Detection game object we will tag it with a new tag “ShotDetection”. This will be needed once we get into the script. And lets jump into the script.
We are going to declare some variables…
- A boolean _isDodging is going to determine when the enemy should start moving.
- An integer _direction will determine if it will move left or right.
- A float that will determine the speed the enemy will move left or right.
A public method (Dodge) is created and in the method the direction will be randomly picked to be with 0 or 1. Also, the StopDodging coroutine will be started.
Let’s move to the Laser script to call this method. We check to see if the Laser is colliding with the game object with the tag of “ShotDetection” and if so, we need to get a handle on our Enemy4 by creating a local variable and using other.GetComponentInParent since the Dodge method is in script that is attached to the Enemy object and not the child object. Do a check null check and call the Dodge method from the enemy script.
Back in the enemy script, once the Dodge method is called it starts the StopDodging coroutine . In this coroutine we set _isDodging to true for .3 of the a second before setting it to false because…
in the Update we are checking to see if _isDodging is True or False. If it is false we set the Translate to a Vector 3 zero, which makes it stop in all direction. If it is true, it will check to see if the direction is 0 or 1. Remember that this was decided when the Dodge method was called.
- 0 = move left
- 1 = move right
Inside that, we are checking the X position and if the enemy ship passes a certain threshold we reposition it to another side. Remember _isDodging is only true for .3 of a second before it is set to false which stops the movement of the enemy. At this moment there is no way to hit this enemy, but I am going to be adding a new enemy fire method which will leave this enemy vulnerable to attack. I hope this article was somewhat helpful to someone. Cheers!