We will create an enemy type that knows when it’s behind the player and fire the weapon backward.
To do it we will use the position on the y axis. If the enemy is behind the player, his transform.position.y will be less than the player.
-2.93 is less than -0.02 so the enemy must be behind the player.
Open the Enemy script and add a new variable called “spawnPos”. This variable will store the position of the laser when its instantiated.
We will also add the “_fireBehind” variable of type bool. We will use it to check if our enemy should fire laser backward.
In the Update() method we will check if the enemy is behind our player. If it is, we will change the value of the “_fireBehind” variable to true.
If we want to fire the laser backward we need to change its position of spawning. Otherwise enemy will kill themselves.
We need to add this variable in the Instantiate() method as a parameter.
Lastly, we need to make sure that the lasers will be moving up when they are fired.