Learn to code by making a 2D space shooter game.

Suleiman Abdullah
3 min readMay 10, 2022

--

Objective: Implement Enemy Avoid Shot.

Before doing anything, drag the new enemy sprite add the box collider rigid body, and a new script.

Note: Set is a trigger to box collider and 0 gravity scale to a rigid body then drag this to the prefab.

In the third step, add movement behavior for this enemy, we move this enemy down when it reaches the bottom we make it appear again above.

Now resize the box collider, we make a collider move forward which gives this enemy to detect a shot early.

Open Enemy avoids Shot script and creates a bool variable called it isShotDetected.

In the next step create a coroutine Called Avoid Shot, this method will be responsible to avoid shots.

In the next step, we need to add behavior to avoid shots, we use the translate method and move the ship right 3 meters then we reset the is Shot Detected so we won't do this forever.

Now we need to set the is Shot detected to true when Laser Hit us, we do this by using OnTriggerEnter2D.

In the next step, go to the Update method to check if is a shot Detected, if true we call AvoidShot.

Let's see the Final Result.

See you in the next article.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development