Making the enemy game object fire sideways in Unity

Samarth Dhroov
4 min readJun 11, 2021

--

The exercise here demonstrates rotating the enemy prefab after it reaches a nearby location to the player and then firing sideways.

Before moving ahead, please make sure you have a new enemy prefab and an associated laser/bullet prefab in place with basic components such as rigid body, box collider, etc.

The pseudo-code is as followed.

  • The distance between the enemy prefab and the player on Y-axis could be achieved by subtracting the player’s position from the enemy’s position. Afterwards, it must be compared to a value in an if statement to begin the rotating movement.
float RotateTest = -1f;
.
.
.
if (transform.position.y - _Playerlocation.transform.position.y < RotateTest)
{
Rotate(); //Start the rotate process.
}

Since this is getting checked every frame, the statement above goes into the update method.

C#

Here, if the x value of the enemy is less than the player’s x value, it means that the enemy is on the left side of the player and hence the rotation is in the anti-clockwise direction. Similarly, the other case is self-explanatory. Then, we are giving value to a variable that basically will change the direction of movement in the update method. (For now, please ignore the laser direction variable.)

  • To check if the prefab was rotated at least once, the mighty bool signal could be used.
bool rotatedOnce = false;
C#

So far the basic code seems in place. Let us make it achieve the task at hand in the first line of shown pseudo-code.

  • The enemy prefab must be instantiated via the spawn manager. I have a wave system configured via scriptable objects. Please have a look here to know more.

So, I have gone ahead and assigned this specific enemy prefab to a wave card to get them instantiated via the spawn manager.

  • As soon as the prefab gets instantiated, it must get its starting value and a movement code in the update method to get it moving.
C#
C#

The _Ydir variable is basically an int with a value -1, which means it is moving in the vector3.down direction. The intention behind doing it this way is to change the value of -1 to 1 when the prefab gets a rotation.

  • The else part should address the enemy prefab’s movement when the distance on Y-axis falls out of the set value. Plus, it must check if the prefab was already rotated before falling out of the set value.

If you go back to the rotate method above, you’d find that there was a _Ydir variable set with a value of 1. Here, we are changing it back to its default value to make the prefab move in the vector3.down manner as soon as the distance condition turns false. But, we are checking in case the prefab was rotated at least once, then there is nothing that needs to be done.

This should conclude the first task of the pseudo-code. Let us address the next part.

  • The associated laser prefab has a basic movement script attached to it.
C#
  • We need a method that interacts with the enemy script and checks for a signal that basically triggers a Z-axis rotation when the enemy itself gets rotated.
C#

If you go back to the rotate method, there is a variable that records a value of 1 or -1, named “laserDirection”. It is actually used in the fireLaser( ) method that instantiates a laser prefab and passes the required value in the method shown just above.

C#

Now, let us complete the last task of the pseudo-code, which is pretty simple. We need to check the values on the X-axis for the enemy and its laser prefab.

if(transform.position.x>11 || transform.position.x < -11)
{
Destroy(this.gameObject);
}
... In the update method of Enemy script & Laser script.

From here, make the necessary connections such as animation, audio upon Ontrigger2D( ) method. Those configurations are always varying hence I’ve left them for you to make.

That should be it :)

Here is the final result.

Thank you very much

--

--

No responses yet