Unity Guide
Implementing space shooter game features - Smart enemy
A quick review of new features added to a space shooter game in Unity
Objective: Implement a smart enemy that shoots when the player is behind in a space shooter game with Unity.
In the previous post I implemented an aggressive behavior for some enemies in my space shooter game with Unity. Now it’s time to implement a smart behavior, which checks if the player is behind the enemy to shoot.
New animation
Like I’ve said in some previous pots, we need to include new sprites to create the new animation for the smart enemy. If you don’t know where to create or obtain new sprites, then I recommend you to use a free and online program (like I did):
Also, If you don’t know how to add sprites or animate them in Unity, you can check these older posts:
Smart shot
To create the animation, let’s create a new animation clip for the enemy using the Animation view in the Unity editor:
Then, let’s identify the respective sprites to use in the clip:
Now let’s drag the sprites into the clip to display the animation as we wish. As you can see in the next images, the enemy will move its weapons behind and then return them to its original position. By using animation events we can make sure to call a function in the enemy script to shoot the laser when the weapons are placed behind:
Then, we need to modify the enemy animation controller in the Animator view, so that the smart shot return to the normal animation and each of them have a connection with the explosion when the enemy is destroyed:
Creating the smart behavior
In order to create the smart behavior, let’s open the Enemy class and add a new variable that stores if the enemy is smart or not:
We use a private variable but a public property to get the value from other classes:
Then, let’s open the EnemyBehavior class and create some new variables to help us handle the smart behavior:
- Delay if smart
This variable will store the time to wait before checking if the player is behind the enemy. By using [SerializeField] we can modify its value through the inspector.
- Smart delay
This will help to handle the respective delay time and avoid wasting memory in the coroutine that handles the smart behavior.
Also, let’s use another variable to store the smart shot prefab reference, which will have another offset to be instantiated:
Then, let’s select the enemy prefab into the scene view and modify its values in the inspector to make it smart:
Now, let’s get back into the EnemyBehavior class to implement the smart behavior. In the Start method we’ll check if the enemy is smart, and if that’s the case we’ll initialize the smart delay using the respective float value and then call the coroutine that checks if the player is behind the enemy:
In the CheckIfPlayerIsBehind coroutine we’ll need a new local variable which will store the length of the Raycast to identify the player collider.
Then, we’ll enter a while loop that will run until the enemy is destroyed. In this loop, we’ll collect all the colliders that are hit by our Raycast using the Physics2D.RaycastAll method. Then, we’ll check all the colliders that were collected and if we identify the player we’ll trigger the smart shot animation and delay the default shot (at the front of the enemy). Finally, we’ll need to return the respective delay until the next Raycast is launched:
Now, let’s create the method that will be called using an animation event in the smart shot animation. This will instantiate the smart shot prefab with an inverse rotation to move into the other way:
And now, in the smart shot animation clip, let’s click on the animation event and select the respective method to instantiate the shot:
Finally, we can drag the smart enemy from the scene view into the respective prefab folder and make it a prefab variant:
Spawning smart enemies
In order to instantiate smart enemies, we need to drag the new prefab into the spawn manager:
Now, if we run the game in Unity, we’ll be able to see that some of the enemies shoot when they detect that the player is behind them:
And that’s it, we implemented a smart enemy! :D. I’ll see you in the next post, where I’ll be showing more features added to my space shooter game in Unity.