Give the Power of Sight to the AI
One of the ways developers can make games fun while challenging is how the function of the enemy. Whether it’s shooting at the player, battling them, or chasing them, by being the enemy, they should do something to hinder the player’s movement towards the goal. Like a see-saw, if one side is heavier than the other side, it creates an imbalance in the system. An imbalance comes the heighten difficultly level (or lack thereof) in a game and with it, comes a decrease in enjoyment of the game. As a result, the balance between the player and the enemy should generally be near the middle.
Seeing the Player
One of the easiest way to create a challenge for the player is to give the enemies a way to “see” the player. By giving the enemies the power of sight, it presents the player with a challenge: should they encounter the problem (the enemy) head-on or devise a way to bypass them? Regardless of what the player does, the execution of the logic is similar.
Simple Way for Sight
Creating a Game Object childed to the Enemy
Although there are several ways to determine if the enemy sees the player, from using Raycast to see if the player crossed path with it to checking the distance between the player and the enemy, the easiest way is to attach a game object to the enemy and use that game object to determine if the player has collided with it.
First, after adding the enemy into the scene, right click on the enemy in the Hierarchy and create a 3D object like a cube (shown left).
This will create the 3D object as the child to the game object. However, if it’s not childed, you can drag the newly created object and place it on top of the enemy.
The reason why we would want to do is is because if the enemy moves, this object will also move it as well.
Resize the Object
Now, it’s time to resize and reposition the game object. For instance, because I want to recreate “sight”, I’ve placed the game object closer to the enemy's eyes and resize it to simulate how far the enemy can see.
After resizing and repositioning, select the child game object and uncheck the Mesh Renderer in the Inspector panel. This will move the material that’s on the game object, thus not allowing the player to see for far the enemy can “see”.
Modifying the Collider and adding Rigidbody and Script
Once you’ve removed the Mesh Renderer on the game object, now it’s time to modify and add more components onto it.
First, check the box next to Is Trigger in the Collider. Mine happens to be a Box Collider due to creating a cube.
Secondly, create a new script and add both the script and Rigidbody onto the game object.
Remember to uncheck the Use Gravity on the Rigidbody so it doesn’t fall to the ground.
Because all we’re doing is determining if the player has crossed path, all we need to do is implement a OnTriggerEnter method in this new script.
If we check to see if the object that’s collided to the created object is the Player via its tag, then do something. In this example, we will play the game over scene.
You have finally given the power of sight to your enemies! Although there are many things you can do when the enemy encounters the player, in this example, the Game Over scene will play automatically.
The Final Product