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.

Line of Sight for Enemies?

I want to be able to set up a situation such as the following.

An enemy paces up and down the map. If you walk within a certain range of his current location (a radius of 6 units or so, representing his "line of sight"), he notices you and the battle begins. However, you can hide behind a box beside him to be hidden from his line of sight, and continue forward.

The box thing will probably be easy (just a switch), but I want to know if there's any way to create the line of sight zone for a given enemy event. (My game does not have random battles, if that confused you).
 
Hm...this depends on what exactly you want to do. There are two general paths you can take.

Eventing: I actually created an Enemy Sight Range tutorial to let an enemy see in front of them. It could be expanded to create a four-directional sight range, but it would be quite complicated. And if you wanted an enemy to be able to see diagonally...well, that would take forever.

Scripting: If you want an actual radius (all directions), you'll want a script. I found two from a quick search -

1. Near Fantastica's View Range (see second post)

2. View Range (Radius) Script

I'm not sure what each of them do since I didn't take the time to read them and their full functionality, but you can always do a further search or make a script request.
 
Thanks very much. Near Fantastica's seems to be the one I want, but it doesn't say anything on how to implement it. I haven't used many scripts in the past so maybe this is common knowledge, but is there anyone who can tell me how?
 
Not sure if the tutorials mentioned explain this, but I've done this many times before by simply using events.

So you have your guard/whoever pacing around. Make a parallel process event that gets his coordinates, then your coordinates, and calculates the difference. Lets say the guard has to be facing you and he can see up to 6 tiles ahead. and about 3 tiles wide.

Guard X - Hero X = Difference

If Difference <= 6
. .Guard Y - Hero Y = Difference
. .If Difference <=3 goto event "Caught you"
. .Else(nothing)
Else(nothing)

That covers that. But what about checking if hes facing you? Easy.

If the guard is facing right, that means if he were to catch you, the Heros X would have to be negative (not positive) since you always subtract the Hero's X from the Guard's. example:

If hero is standing at X=7 and Guard is at X=5, then (5-7) is -2. So you know the Hero is to the right of the guard. If hero was at 5 and guard at 7, then (7-5) = 2 and youd know the hero is to the left.

So

If Sprite (Guard) is Right Facing:
. .If Guard X-heroX >= -6 and <0
. .then (caught)

hope ive explained myself, I use this system flawlessly for all my events of this nature.
 
They should work with pixel movement. The only stipulation being that if the objects .x property returns in pixels instead of tiles, you'd have to input the range in pixels as well. Try it out.
 
K, I tried Monsta's initially but I ran into some bugs. So I switched over to the newer version of Seph/Near's. However, as soon as I boot up the game, it says there's a syntax error in line 30. It says:

uninitialized constant MACL

??? I don't understand what it's telling me. Anyone, help?

EDIT: The script.

Code:
 

   1. #==============================================================================

   2. # ** Modules.View Range (5.0)               By Near Fantastica & SephirothSpawn

   3. #------------------------------------------------------------------------------

   4. # * Description :

   5. #

   6. #   The View Range Module is used to test objects position in comparision with

   7. #   another object. It can test if an object is within a defined circular

   8. #   range of another object or a rectangular defined region. It can also tell

   9. #   you this distances between objects.

  10. #------------------------------------------------------------------------------

  11. # * Syntax :

  12. #

  13. #   Test if object is within defined range from another object :

  14. #    - VR.in_range?(object1, object2, range)

  15. #    - VR.in_range?(object1x, object1y, object2x, object2y, range)

  16. #

  17. #   Test if object is within defined rectanlge :

  18. #    - VR.in_rect_range?(object, <args>)

  19. #    - VR.in_rect_range?(x, y, <args>)

  20. #

  21. #    args can either be : x, y, width, height or Rect.new(x, y, width, height)

  22. #

  23. #   Test range between objects :

  24. #    - range = VR.range(object1, object2, <integer>)

  25. #    - range = VR.range(object1x, object1y, object2x, object2y, <integer>)

  26. #

  27. #    integer : if true, returns integer ; if false, returns float

  28. #==============================================================================

  29.  

  30. MACL::Loaded << 'Modules.View Range'

  31.  

  32. #==============================================================================

  33. # ** View Range

  34. #==============================================================================

  35.  

  36. module VR

  37.   #----------------------------------------------------------------------------

  38.   # * Within Range Test

  39.   #----------------------------------------------------------------------------

  40.   def VR.in_range?(*args)

  41.     # If 3 Arguments (Element, Object, Range)

  42.     if args.size == 3

  43.       x = (args[0].x - args[1].x) ** 2

  44.       y = (args[0].y - args[1].y) ** 2

  45.       r = args[2]

  46.     # If 5 Arguments (Elementx, Elementy, Objectx, Objecty, Range)

  47.     elsif args.size == 5

  48.       x = (args[0] - args[2]) ** 2

  49.       y = (args[1] - args[3]) ** 2

  50.       r = args[4]

  51.     else

  52.       p 'Wrong Defined Number of Arguments'

  53.       return

  54.     end

  55.     return (x + y) <= (r * r)

  56.   end

  57.   #----------------------------------------------------------------------------

  58.   # * Within Rect Range Test

  59.   #----------------------------------------------------------------------------

  60.   def VR.in_rect_range?(*args)

  61.     # If 2 Arguments (Object, Rect)

  62.     if args.size == 2

  63.       x_, y_ = args[0].x, args[0].y

  64.       x, y, w, h = args[1].x, args[1].y, args[1].width, args[1].height

  65.     # If 3 Arguments (Objectx, Objecty, Rect)

  66.     elsif args.size == 3

  67.       x_, y_ = args[0], args[1]

  68.       x, y, w, h = args[2].x, args[2].y, args[2].width, args[2].height

  69.     # If 5 Arguments (Object, Rectx, Recty, Rectwidth, Rectheight)

  70.     elsif args.size == 5

  71.       x_, y_ = args[0].x, args[0].y

  72.       x, y, w, h = args[1], args[2], args[3], args[4]

  73.     # If 6 Arguments (Objectx, Objecty, Rectx, Recty, Rectwidth, Rectheight)

  74.     elsif args.size == 6

  75.       x_, y_, x, y, w, h = *args

  76.     else

  77.       p 'Wrong Defined Number of Arguments'

  78.       return

  79.     end

  80.     # Return Object Within Rect

  81.     return x_.between?(x, x + w) && y_.between?(y, y + h)

  82.   end

  83.   #----------------------------------------------------------------------------

  84.   # * Range

  85.   #----------------------------------------------------------------------------

  86.   def VR.range(*args)

  87.     # If 2 Arguments (Element, Object)

  88.     if args.size == 2

  89.       x = (args[0].x - args[1].x) * (args[0].x - args[1].x)

  90.       y = (args[0].y - args[1].y) * (args[0].y - args[1].y)

  91.       integer = true

  92.     # If 3 Arguments (Element, Object, Integer

  93.     elsif args.size == 3

  94.       x = (args[0].x - args[1].x) * (args[0].x - args[1].x)

  95.       y = (args[0].y - args[1].y) * (args[0].y - args[1].y)

  96.       integer = args[2]

  97.     # If 4 Arguments (Elementx, Elementy, Objectx, Objecty)

  98.     elsif args.size == 4

  99.       x = (args[0] - args[2]) * (args[0] - args[2])

 100.       y = (args[1] - args[3]) * (args[1] - args[3])

 101.       integer = true

 102.     # If 5 Arguments (Elementx, Elementy, Objectx, Objecty, integer)

 103.     elsif args.size == 5

 104.       x = (args[0] - args[2]) * (args[0] - args[2])

 105.       y = (args[1] - args[3]) * (args[1] - args[3])

 106.       integer = args[4]

 107.     else

 108.       p 'Wrong Defined Number of Arguments'

 109.       return

 110.     end

 111.     r = Math.sqrt(x + y)

 112.     return integer ? r.to_i : r

 113.   end

 114. end

 
 

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