The "Don't Look at Me" Challenge: Mastering Line-of-Sight in Verse
Tutorial beginner

The "Don't Look at Me" Challenge: Mastering Line-of-Sight in Verse

Updated beginner

The "Don't Look at Me" Challenge: Mastering Line-of-Sight in Verse

So you want to build a trap that only triggers if the enemy is actually staring at it? Or maybe you’re making a stealth game where guards only shout "I see you!" when their eyes lock onto the player. That’s called Line-of-Sight (LOS) checking. It’s the digital equivalent of peeking around a corner to see if anyone’s there.

In this tutorial, we’re going to build a "Spyglass Trap." If a player looks directly at a specific spot in the air for more than a second, a confetti cannon explodes. If they look away? Nothing happens. No magic, no guessing—just pure vector math disguised as gameplay.

What You'll Learn

  • Vectors: The GPS coordinates of your game world.
  • Dot Product: A math trick to calculate if two directions are aligned (like aiming a crosshair).
  • Events & Loops: How to make your device "watch" players continuously.
  • Scene Graph Basics: How devices talk to characters and the world.

How It Works

Before we write code, let’s translate this into Fortnite logic.

Imagine you are holding a sniper rifle. You have a crosshair (your View Direction). There is a target on the horizon (the Target Location). To know if you are looking at the target, you don’t need to know the exact distance. You just need to know if your crosshair is pointing in the same general direction as the target.

In programming, we use a Vector to represent direction. Think of a Vector like an arrow sticking out of your head.

  • View Direction: The arrow pointing where your camera is facing.
  • Target Direction: An arrow pointing from your head to the trap location.

If these two arrows point in the exact same direction, the angle between them is 0 degrees. If they point opposite ways, it’s 180 degrees. We use a math operation called the Dot Product to measure this.

  • Dot Product = 1.0: Perfectly aligned. You are looking right at it.
  • Dot Product < 1.0: You are looking slightly away.
  • Dot Product < 0.0: You are looking behind you.

We’ll set a "Threshold" (like a tolerance level). If the Dot Product is high enough (e.g., > 0.95), we count it as "Looking At It."

Let's Build It

We need a device that runs in a loop, checking every player every frame.

The Setup

  1. Place a Creative Device in your island.
  2. Name it SpyglassTrap.
  3. Open the Verse editor for that device.
  4. Copy the code below.

The Code

using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /Verse.org/Simulation }

# This is our device class. Think of it as the "brain" of the trap.
spyglass_trap_device := class(concrete creative_device):
    
    # @editable variables appear in the device settings in UEFN.
    # Threshold: How precise the "looking" needs to be. 
    # 0.95 = very precise (narrow cone), 0.5 = wide angle.
    @editable
    LookThreshold: float = 0.95
    
    # The location we want players to look at.
    # We will set this via code to a fixed point in the sky.
    TargetLocation: vector3 = vector3(0.0, 0.0, 1000.0)
    
    # This function runs automatically when the game starts.
    OnBegin<override>()<suspends>: void =
        # Infinite loop! This keeps checking forever.
        loop:
            # Get all players currently in the game.
            # GetPlayers() returns a list of all active players.
            players := Self.GetPlayspace().GetPlayers()
            
            for player in players:
                # Get the character model for this player.
                # Not all players might have a character loaded yet, so we check.
                if (character := player.GetFortCharacter()):
                    
                    # 1. Get where the player's camera is.
                    view_location := character.GetViewLocation()
                    
                    # 2. Get the direction the player is facing.
                    # GetViewRotation() gives us the angle. 
                    # We convert that angle into a direction vector (an arrow).
                    view_direction := character.GetViewRotation().GetForwardVector()
                    
                    # 3. Calculate the direction FROM the player TO the target.
                    # Vector subtraction: Target - Player = Direction to Target.
                    to_target := TargetLocation - view_location
                    
                    # Normalize turns that long arrow into a standard-length arrow (length 1).
                    # This ensures distance doesn't mess up our angle check.
                    to_target := Normalize(to_target)
                    
                    # 4. The Magic Math: Dot Product.
                    # Dot product of two normalized vectors tells us how aligned they are.
                    alignment := Dot(view_direction, to_target)
                    
                    # 5. Check if aligned enough.
                    if alignment > LookThreshold:
                        # BINGO! They are looking at the spot.
                        # Let's make a sound or visual effect.
                        # For simplicity, we'll just print to the debug log.
                        Print("Player is staring at the target!")
                        
                        # Optional: Add your trap trigger here!
                        # e.g., SpawnEffectAtLocation(...)
                    
                    # Small pause so we don't crash the game with 1000 checks per second.
                    # 0.1 seconds = 10 checks per second. Smooth enough.
                    await Delay(0.1)

Walkthrough: What Just Happened?

  1. OnBegin: This is your "Game Start" button. It kicks off the loop. In Verse, loop is like an infinite timer that never stops until the game ends.
  2. GetPlayers(): This grabs everyone in the lobby. If you have 10 players, this list has 10 items.
  3. GetViewLocation() & GetViewRotation(): These are the "eyes" of the player. One gives the GPS coordinate of their head; the other gives the angle they are facing.
  4. GetForwardVector(): This converts the angle into a Vector (a direction arrow). Imagine an arrow shooting out of your camera lens.
  5. TargetLocation - view_location: This is vector subtraction. If you are at (0,0,0) and the target is at (10,0,0), the result is (10,0,0). This is the direction from you to the target.
  6. Normalize(): This is crucial. If you are 1 meter away, the arrow is short. If you are 100 meters away, the arrow is long. We shrink both arrows to length 1.0 so we only care about direction, not distance.
  7. Dot(): This multiplies the two direction arrows together.
    • If you look straight at the target, Dot returns 1.0.
    • If you look 90 degrees away, Dot returns 0.0.
    • If you look behind, Dot returns -1.0.
  8. if alignment > LookThreshold: If the result is close to 1.0, you’re looking at it!

Try It Yourself

Challenge: The current code triggers a message whenever any player looks at the target. Modify the code so that it only triggers if the player is within 50 meters of the target location.

Hint: You already calculated the distance in step 3 (to_target). After normalizing, you lost the distance info. Before you normalize to_target, check its length using .Length(). If the length is greater than 50.0, skip the check.

<details> <summary>💡 Hint Solution</summary>

</details>

Recap

  • Vectors are arrows that represent position and direction.
  • Dot Product tells you how aligned two directions are (1.0 = perfect aim).
  • Normalize makes sure distance doesn’t interfere with your angle calculation.
  • Loops allow your device to constantly monitor players in real-time.

Now go build a trap that only explodes if they dare to look at it.

References

  • https://dev.epicgames.com/community/snippets/mwza/fortnite-how-to-check-if-a-player-is-looking-at-a-location
  • https://dev.epicgames.com/documentation/en-us/fortnite/prop-hunt-03-playing-effects-on-idle-players-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/fortnite/objective-marker-gameplay-tutorial-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-player-spawn-pad-devices-in-fortnite-creative
  • https://dev.epicgames.com/documentation/en-us/uefn/verse-prop-hunt-template-3-playing-effects-on-idle-players-in-unreal-editor-for-fortnite

Get the complete code — free

You've read the full walkthrough. The complete, copy-paste-ready Verse solution is free for members — sign in to unlock it.

Free with your BrainDead.TV / BrainDeadGuild Discord account. The walkthrough above is always free.

Verse source files

Turn this into a guided course

Add fortnite-how-to-check-if-a-player-is-looking-at-a-location to your free study plan — we'll suggest related pages and stitch the lot into one compile-checked, self-guided lesson with worked examples and quizzes.

Original tutorial generated by Verse Island from the Verse/UEFN knowledge base, with references to the Epic Games sources above. Code is validated against the knowledge base.

Comments

    Sign in to vote, comment, or suggest an edit. Sign in