
RPG Maker MZ/MV Expert Tutorial
Line of Sight
Line of Sight isn't difficult, but the lines of code can get lengthy. Don't let that intimidate you. By the end of this series, you will be able to create Lines of Sight of all aura types (circular, linear, quadratic, etc.) and easily access it through common events.
Below are the copy-paste and easy-to-read versions to better your understanding. Use the navigational tool to quickly access each part.

Line of Sight Part 1 - Everything Except Circles
Copy & Paste Code:
Spotted in Facing Direction (Copy Pasted as 1-Line)
$gameMap.event(this._eventId).direction() == 2 && $gameMap.event(this._eventId).y < $gamePlayer.y || $gameMap.event(this._eventId).direction() == 8 && $gameMap.event(this._eventId).y > $gamePlayer.y || $gameMap.event(this._eventId).direction() == 4 && $gameMap.event(this._eventId).x > $gamePlayer.x || $gameMap.event(this._eventId).direction() == 6 && $gameMap.event(this._eventId).x < $gamePlayer.x
​
Triangle Vision (Copy Pasted as 1-Line)
Math.abs($gameMap.event(this._eventId).x - $gamePlayer.x) + Math.abs($gameMap.event(this._eventId).y - $gamePlayer.y) <= 3
​
Line Vision (Copy Pasted as 1-Line)
Math.abs($gameMap.event(this._eventId).y - $gamePlayer.y) <= 3 && $gameMap.event(this._eventId).x == $gamePlayer.x || Math.abs($gameMap.event(this._eventId).x - $gamePlayer.x) <= 3 && $gameMap.event(this._eventId).y == $gamePlayer.y
​
Box Vision (Copy Pasted as 1-Line)
Math.abs($gameMap.event(this._eventId).y - $gamePlayer.y) <= 3 && Math.abs($gameMap.event(this._eventId).x - $gamePlayer.x) <= 1 || Math.abs($gameMap.event(this._eventId).x - $gamePlayer.x) <= 3 && Math.abs($gameMap.event(this._eventId).y - $gamePlayer.y) <= 1
Easy to Read Code (Click to Enlarge):
*Note: Do not copy the code below. Use Copy & Paste Code above


Line of Sight Part 2 - Circles
Everything in this box is for Circles Only
Use Below Image Key to Better Understand Code (Click to Enlarge)

Key
A1 & A2 - Aura radius and test in aura respectively
B - Test whether difference in X or Y is smaller. Smaller X goes to Cs, Larger X goes to Ds.
C1 - Test difference in X is less than/equal to radius (A1)
C2 - Pythagorean theorem to solve for Y and test if the difference in Y is less than/equal to radius.
D1 - Test difference in Y is less than/equal to radius (A1)
D2 - Pythagorean theorem to solve for X and test if the difference in X is less than/equal to radius.
E1 & E2 - Tells the event that player is within aura, otherwise the switch remains OFF as in A2.
E3 - When player is in aura, do the following.
​
​
​
Copy & Paste Code
B:
Math.abs($gamePlayer.x-$gameMap.event(this._eventId).x) < Math.abs($gamePlayer.y-$gameMap.event(this._eventId).y)
C1 (Note change [$gameVariables.value(2)] as appropriately):
Math.abs($gamePlayer.x - $gameMap.event(this._eventId).x) <= $gameVariables.value(2)
C2 (Note change [$gameVariables.value(2)] as appropriately):
Math.abs($gamePlayer.y-$gameMap.event(this._eventId).y) <= Math.round( Math.sqrt(Math.pow($gameVariables.value(2),2) - Math.pow($gamePlayer.x - $gameMap.event(this._eventId).x, 2)) )
​
D1 (Note change [$gameVariables.value(2)] as appropriately):
Math.abs($gamePlayer.y - $gameMap.event(this._eventId).y) <= $gameVariables.value(2)
​
D2 (Note change [$gameVariables.value(2)] as appropriately):
Math.abs($gamePlayer.x-$gameMap.event(this._eventId).x) <= Math.round(Math.sqrt(Math.pow($gameVariables.value(2),2) - Math.pow($gamePlayer.y - $gameMap.event(this._eventId).y,2)) )
Easy to Read Code (Click to Enlarge):
*Note: Do not copy the code below. Use Copy & Paste Code above
