CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Follow publication

Creating a basic field of vision system in Unity/C#

Let’s setup a basic circular or conical FOV for a little guard!

Mina Pêcheux
CodeX
Published in
16 min readApr 13, 2023

--

There are lots of games where enemies will spot you only when you enter a specific area around them. This concept of a “field of vision” may not be the most realistic thing, but it is nonetheless a very common one.

And guess what? It’s actually not that hard to make in Unity/C#!

This tutorial is available in video or text format — see below for the text version :)

Check out the Github repo with all my Unity tutorials! 🚀

So, in this tutorial, we’ll work on giving a robot guard unit a limited awareness of its surroundings by defining a field of vision, or FOV, both as a circle and as a cone:

We’ll see how to use this zone as a trigger for the guard’s different alertness levels, and we’ll also discuss how to use Unity’s editor tools to create our own visualisation gizmos.

And with all that said, let’s get to it and discover how to setup a field of vision system in Unity/C# :)

👁 Defining & visualising the FOV

Alright — the first thing we’re going to work on is defining the field of vision of our guard as a circular area around its position. So we’re going to assume that we have this basic scene with the unit 3D model in the middle:

And we’ll setup a simple C# script to handle the guard’s logic, which here will mostly be about the FOV system and related attention levels. We’ll name the script GuardManager.cs, and slap it on our game object in the scene:

--

--

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Mina Pêcheux
Mina Pêcheux

Written by Mina Pêcheux

I’m a freelance full-stack web & game developer. I’m passionate about topics like CGI, music, data science and more! Find me at: https://minapecheux.com :)

No responses yet

Write a response