Unity Playmaker - Third Person - Enemy AI - Line of Sight
In this 3rd Person in Unity and Playmaker tutorial, I'll be showing how to set up the Enemy AI Line of Sight (if the enemy can see the player or is player behind an object). ► Download Playmaker at https://bit.ly/3dV8JzC ► Download Unity at https://unity.com Chapters: 00:00 Introduction 00:45 Addi
The enemy AI series has covered roaming, range detection, and field of view — but so far the spider would "see" the player through a rock. This tutorial adds true line of sight: a raycast that confirms the player is actually visible, not just nearby, with nothing solid in the way.
Add an eye object
Inside the spider, create an empty game object called line of sight, positioned about where the eyes would be. This exists because the setup uses the Look At action — and pointing Look At at the spider's body would physically rotate the whole spider toward the player. A child object can swivel freely to track the player without dragging the body around with it.
Find the player and track them
Add an FSM named line of sight with a first state called detect player. Because the spider is a prefab, any scene reference to the player breaks when the prefab is re-dragged in — so use Find Game Object with the player tag and store the result in a player variable. Then add Look At on the line of sight object, targeting that variable. Two adjustments matter: set the target position's Y offset to 1 so the eye aims at the player's middle rather than their feet, and uncheck keep vertical, otherwise the look direction stays horizontal and misses a player above or below. Check every frame.
Raycast and compare the hit
A raycast is a laser line: whatever it hits first is what the enemy sees. Add Raycast from the line of sight object along positive Z in self space — straight out the front of the eye, which Look At keeps aimed at the player. Distance of 10 is plenty when detection range is a 5-meter radius. Instead of a hit event, use store hit game object into a hit object variable, then add Game Object Compare Tag on hit object against the player tag, every frame. On true, send a player detected event to a new state — if the ray hit the player first, nothing is blocking the view.
Mirror the logic for losing sight
The player detected state reuses the same actions — paste the Look At, Raycast, and Compare Tag (no need to find the player again). Flip the compare: uncheck the true event and fire player not detected on false, transitioning back. The two states are mirror images, each watching for the moment the answer changes. Test it: the spider sees the player immediately, and sliding a rock between them breaks the line of sight. Combined with the previous two tutorials, this completes the sensing layer — next comes the reaction: chasing and attacking.





