Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Detection/Sneaking System - Line of Sight

Tuna

Awesome Bro

Here's something I whipped up a while ago. It gives an event (like a guard) a line of sight (i.e. 1-5 squares in front of itself) and it detects the player if they walk into the event's line of sight. The result can be modified from there. This even works if the event is moving, but it requires some finesse to keep it from looking through walls, etc. This can be done by adjusting its motion path or setting a particular case if the event as at a certain problem location. You can download the project at:
http://www.4shared.com/file/129240096/94caef4a/Guard_Test.html.

Basically I set Hero X and Y variables and Event X and Y variables. I put all of the coding inside the guard so it's easy to keep track of, and you can set its variables more easily (This Event's...). Here's the technical stuff:
iseeyoucode.png
Alternatively, here's a snippet of the code:

If facing left...
If Player Y == Event Y
If Player X < Event X
Set Hero X Check = Player's Map X
Set Event X Check = Event's Map X
Event X Check - Hero X Check
If Event X Check >= 1(Greater or Equal)
If Event X Check <= 5(Less or Equal)
Text: I see you!
Endif (Branch End)
Endif
Endif (Player X < Event X)
Endif (Player Y == Event Y)
Endif (facing left)


This will make the guard notice the player if they are within 5 tiles of the guard. The reason for setting the checks is due to the fact that you can't check differences, only one variable. To solve this, I made Check variables that check the player and event's current locations, subtract, and see if the result is within a range of 5. IT IS IMPORTANT to make these variables separate from the Hero X/Y and Event X/Y variables because every guard event that you have running on the map needs to run this check, and they can't all be subtracting and testing the same variables.

Using this system of checks, you can give your guards any motion path you wish, but you will have logic problems if there is an object between the guard and the player, because the guard will not detect the object. To get around this you can set your guards' motion paths so that they will not be in a position to look through objects, you can adjust the ranges if you know a stationary guard will be looking through an object in one direction, or you can set for a special check if the guard is at Position X/Y.

If you would like to change the guard's vision range, simply adjust the final check,
If Event X Check <= 5(Less or Equal).
If you wanted the guard to see 10 tiles ahead, change 5 to 10. If you'd only like the guard to see 3 tiles ahead, change 5 to 3, etc.

If anyone notices any problems with my code, please let me know. It worked fine when I tested it, but I'm not perfect. Good luck with your projects!
 
I formatted your code so it's easier to see the logic.

1) If Player Y == Event Y

will only work (return true) when the player is in the same row as the event. This may have been your intention. But it's not very realistic if the player is, for example 5 tiles to the left and one tile up, the guard would probably still be able to see him.

2) If Event X Check >= 1(Greater or Equal)

What does this do? I'm not refuting it, just asking you to state it's purpose.


Still, a good start on an example of how to use multiple variables & conditional statements to solve a problem. Thanks for contributing.

Be Well
 

Tuna

Awesome Bro

Brewmeister":53ubig0s said:
1) If Player Y == Event Y

will only work (return true) when the player is in the same row as the event. This may have been your intention. But it's not very realistic if the player is, for example 5 tiles to the left and one tile up, the guard would probably still be able to see him.
This is true. My only problem is getting the player to understand a wider range of sight and avoid it. A more realistic approach would be to have a check for a cone of vision (like the Metal Gear series), but this would have to be made clear to the player (like the radar in Metal Gear.) This would probably be effective if say the event was in a dark room and had a flashlight equipped, and you could see that you'd be detected if you walked into the light.

Brewmeister":53ubig0s said:
2) If Event X Check >= 1(Greater or Equal)

What does this do? I'm not refuting it, just asking you to state it's purpose.
Good find. After looking back at it, I realized that was just an unnecessary check. The only way it would be less than 1 would be if the player was on top of or behind the event, and that would already be disregarded in an earlier check. I went through and fixed that. New version is now uploaded.

Thanks for the feedback!
 
2) one possible explanation would be to discontinue the checking once they are adjacent. But you would most likely do this as soon as the player is detected.

To Check a "Cone" (45 degrees left & right of line of sight), when the event is facing left....

if Event_x > Player_x
if (Event_x - Player_x) >= (Event_y - Player_y)
# player is within the cone
endif
endif
 

Tuna

Awesome Bro

Hmm... I can't really see how that works. I went and made a new guard with a cone feature that will detect two squares in front, two squares diagonal to its left and right, and the rest that's in between.

Explanation (Face Left)
First it does the standard check in the original version to see if the player is standing directly in front of it. Then it checks to see if Player X < Event X. If so, then it conducts a Y Check. Then, if X Check = 1, Y Check = +-1, it sees the player. If X Check = 2, Y Check = -2,-1,1,2, it sees the player. I uploaded this new event (works in all directions).

In addition to this, I took the individual variable sets (hero x = hero x check, etc.) and put them all at the start of the event. I realized it wasn't necessary to put a variable set within each condition (L,R,U,D).
 
Ah, I needed an "absolute value" in there... and the 5 tile distance check

If you draw a cone facing to the left (45 deg up left, and 45 deg down left), any point inside the cone will have an X distance from the focal point larger than it's absolute Y distance. Points directly on either line will have X = |Y|

if Event_x > Player_x
if (Event_x - Player_x) >= |(Event_y - Player_y)|
# player is within the cone
if X Check <= 5
# Spotted
endif
endif
endif


eventpff.png
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top