Tip of the Day: Give your AI Sight in Unity
Today’s tip is a quick one. AI sight is most useful in stealth games but it can be implemented in many other areas.
We will create a very simple and effective way of doing so. But if you want a more precise eyesight, then you can use Raytracing (we might see how it is done later)
Objective: Let your AI see
First off let’s create an empty game object on the enemy AI and call it Eye Sight.
For this method to work we will need to add a collider to act as a trigger and a kinematic rigidbody component. Also create a C# script and attach it to the enemy AI.
Scripting this is fairly straightforward. We will take advantage of the On-trigger method of Unity to detect if the box collider of the eye sight game object has collided with our player for example. And only you can do whatever you want when this collision happens.
Adjusting the eyesight distance is as easy as editing the collider. We used a box collider, but you can use whatever collider you see best for you project.
Here it is in action after the Enemy AI detects the Player using this method.
Using the above method we can also let our security cameras detect the player and trigger our captured cutscene.