Creating A Cone Of Vision To Let Your AI See

I’ve gone into this a bit in a previous article, but today let’s address the code.

When you are creating AI for your game, you may want them to simulate sight so as to give the feeling that they can see what is going on around them. This is of course all smoke and mirrors as are most things in the world of game development, but if done correctly, it can make your game feel ALIVE!

The concept of creating a cone of vision is about as simple as the code needed to implement it. Which is good news to all of you non-programmers out there just trying to prototype/flesh out their game.

To create a line of sight you can use Raycasting, which is a little more complex, or you can use colliders. I will make an article on Raycasting to see your targets eventually, but today it’s just colliders.

You will need to create an empty child object under your AI and add a collider of some form to it. Then stretch it out in front of your AI according to your preferences. These are the boundaries that determine what they can and cannot see.

If you are wanting to do multiple lines of sight to represent the peripheral vision or something else the do so now.

[Important]

Make sure you set your colliders to “Trigger”.

Now for the script. Create a new C# script and attach it to the new game object you made that contains the colliders for the AI’s cone of vision.

For my game, I activated a cutscene depicting a game over sequence after the player is detected. But you could use this same reasoning to trigger combat, dialogue, or whatever after the enemy sees the player.

If you created extra cones of vision for the peripheral detection, then you just need to repeat the steps above but on a separate game object than the first and trigger a different state for your AI such as begin searching for the player, or whatever you are wanting.

--

--

--

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Business Growth in SEA and the potentials of Chatbot

Oxford Martin School series — “Data work: the hidden talent and secret logic fuelling artificial…

Custom Vision AI — Building Your Own Custom Model and Train

Introducing the XaiPient blog

Learning French with Alexa

Photo of Amazon Echo Dot 2, on coffee table with books

How AI Can Solve Problems for Gym/Studio Owners in 2020

AI and Ethics

Top 10 Ways an Educational Chatbot Can Revolutionize Your Business

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
Adam Reed

Adam Reed

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

More from Medium

Trigger collisions

ON YOUR MARKS…

Initializing A Git Repository in Unity

New Input System and UI