Nerd For Tech
Published in

Nerd For Tech

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.

Enemy AI sees player, player gets caught.

Using the above method we can also let our security cameras detect the player and trigger our captured cutscene.

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Apache APISIX 2.9 is officially released, bringing more new features!

Announcing open source Faust Prometheus library

Elegantly Handling Constant, Large Scale Updates

Soft skills for programmers.

How to Make an e-Learning Platform

SFCC - Automating On-Demand Sandboxes

Migrating from state_machine to aasm in Rails

What is Directus?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mohamed Hijazi

Mohamed Hijazi

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/

More from Medium

Cookies and Lights in Unity 2020

In 6 steps, Add a double jump in Unity

My journey becoming a Unity game developer: Sleeping Guard Interaction-Player NavMesh movement…

Learn to code by making a 2D space shooter game.