Skip to main content
‹ Back to Tutorials
Intermediate Unity

Unity Playmaker - 3rd Person - Enemy AI - Field of View

Jun 2, 2022
About this tutorial

In this 3rd Person in Unity and Playmaker tutorial, I'll be showing how the enemy AI can detect if the player is within the enemies Field of View. This is the second part of multiple player detection tutorials (so stay tuned for Line of Sight and Enemy Reactions tutorials). ► Download Playmaker at

Written Guide

This entry in the third-person enemy AI series gives the spider enemy a field of view — a cone of vision that decides whether the player is seen or unseen. It's built as its own small FSM on purpose: range detection, field of view, and (next episode) line of sight each live in separate state machines, and later they combine into the full detection brain. Small parts are easier to manage than one giant FSM.

Step 1 — Find the player the prefab-safe way

Add a new FSM to the enemy named Field Of View, with a first state called Detect Player. Here's the catch with prefabs: the spider will be one, and if you drag the player directly into an action's target slot, that reference dies the moment the prefab is re-instantiated. So instead, add a Find Game Object action before anything else — leave the object name blank, search by the Player tag, and store the result in a new variable called player. (A global variable would also work, but tag lookup is easy and there's only one player.) Now every spawned spider finds the player on its own.

Step 2 — Measure the angle to the player

Add a Get Angle To Target action using the player variable as the target, and store the result in a new float named angle to player. This gives you how far off the spider's facing direction the player currently is, anywhere in a full 360 degrees. Check Every Frame — vision has to update continuously, not once.

Step 3 — Compare against the view cone

Add a Float Compare between angle to player and 75, with tolerance 0, every frame. The 75 is measured from the spider's center line to each side, so it actually defines a 150-degree cone — tune it per creature; a wide-eyed enemy might see nearly everything while a mole would get a sliver. Fire a new event, player detected, on both Equal and Less Than, and transition to a Player Detected state. Equal-or-less means inside the cone.

Step 4 — Detect losing sight, and loop

The Player Detected state is the same logic mirrored. Reuse Get Angle To Target (no need to find the player again — the variable's already stored) and a Float Compare against 75, but this time fire a player not detected event on Greater Than, transitioning straight back to Detect Player. That gives you a clean two-state loop: watching for the player, seeing the player, and flipping between them as the angle crosses 75 degrees. Colorize the states — orange for detected — so the flip is obvious in the Playmaker window.

Step 5 — Test it live

Press play, lock the Playmaker window to the spider, then grab the player in the scene view and drag it around the enemy. The FSM should ping between states as you cross the cone's edge: in view, out of view, in view again. Combined with the range check from the previous episode, the spider now knows both how far and how wide it can see — next up is line of sight, so a big rock between spider and player actually blocks the view.

Unity tools by Bliz Studio
Built for our own games — now on the Asset Store

Multi Tag — unlimited hierarchical tags for any GameObject. Hierarchy Pro — style your whole hierarchy with rules. Made by a Unity dev, for Unity devs.

Explore our Unity assets →