enemy detection on map and zombie behaviors.

I have seen a lot of Youtube videos and demos that make it look like enemies are chasing on you the map. i just can't perfect it to where they actually have to touch you in order to engage in 

combat. They will approach from two squares and then they'll still tag me somehow, and if two enemies are nearby on the screen even after a battle the game locks up.

here is the code I used as :

Proximity Detection Script v1.5

 

module Proxy

  #Default radius of detection:

  PROXYRANGE = 4

  #Region for transparent obstacles

  REGION = 20

  #Switch to be turned on to pause Proximity

  PAUSE_SWITCH = 100

  #----#

  def self.inprox?(id, distance = PROXYRANGE, los = true, second_id = nil)

    return if $game_switches[PAUSE_SWITCH]

    x = $game_map.events[id].x

    y = $game_map.events[id].y

    if !second_id

      x2 = $game_player.x; y2 = $game_player.y

    else

      x2 = $game_map.events[second_id].x;y2 = $game_map.events[second_id].y

    end

    x_d = x - x2; y_d = y - y2

    x_d *= -1 if x_d < 0; y_d *= -1 if y_d < 0

    t_d = x_d + y_d

    return false if t_d > distance

    return false if !line_of_sight($game_player.x,$game_player.y,x,y) and los

    return true

  end

  def self.inprox_d?(id, distance = PROXYRANGE, los = true)

    if self.inprox?(id, distance, los) then else return false end

    x1 = $game_player.x; x2 = $game_map.events[id].x

    y1 = $game_player.y; y2 = $game_map.events[id].y

    x1 > x2 ? xx = x1 - x2 : xx = x2 - x1

    y1 > y2 ? yy = y1 - y2 : yy = y2 - y1

    case $game_map.events[id].direction

    when 2

      if $game_player.y > $game_map.events[id].y then

        if yy >= xx then return true end end

    when 4

      if $game_player.x < $game_map.events[id].x then

        if xx >= yy then return true end end

    when 6

      if $game_player.x > $game_map.events[id].x then

        if xx >= yy then return true end end

    when 8

      if $game_player.y < $game_map.events[id].y then

        if yy >= xx then return true end end

    end

    return false

  end

  def self.inprox_r?(id, width, height)

    return if $game_switches[PAUSE_SWITCH]

    width % 2 == 0 ? hwidth = width / 2 : hwidth = (width - 1) / 2

    height % 2 == 0 ? hheight = height / 2 : hheight = (height - 1) / 2

    x = $game_map.events[id].x - hwidth

    y = $game_map.events[id].y - hheight

    if $game_player.x >= x and $game_player.x < (x + width)

      if $game_player.y >= y and $game_player.y < (y + height)

        return true

      end

    end

    return false

  end

  def self.line_of_sight(x,y,x2,y2)

    tile_array = []

    x_d = x - x2; y_d = y - y2

    x_d *= -1 if x_d < 0

    y_d *= -1 if y_d < 0

    t_d = x_d + y_d

    t_d.times do |i|

      x_distance = x - x2

      y_distance = y - y2

      x_distance *= -1 if x_distance < 0

      y_distance *= -1 if y_distance < 0

      if x_distance > y_distance or y_distance == 0

        x < x2 ? x += 1 : x -= 1

      elsif 

        y < y2 ? y += 1 : y -= 1 or x_distance == 0

      end

      tile_array.push([x,y])

    end

    tile_array.each do |cord|

      next if $game_map.region_id(cord[0],cord[1]) == REGION

      return false if !$game_map.check_passage(cord[0], cord[1], 0x002)

    end

    return true

  end

end

 

I am not knowledge in ruby as of yet, I got this from a youtube channel actually. But if there is a better way to fix this or a better method to make enemies have to touch the player directly in order for

combat to engage. I also need help with a behavior for zombies to be attracted to players from a distance.

 

thanks in advance as always.
It would probably be better to use a script that wasn't posted on youtube. Not that there's anything wrong with it, but youtbe could have changed the format for key spots and that can throw the entire script off. There are a few scripts that do what you want. Popular ones that I've seen is Yanfly and MOG-Hunter's event chaser scripts. They can be found with a quick google search.
I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


Next time you ask for help with scripts, please post a LINK to where you got the script from, rather than the script itself (preferably to a forum post or blog article that contains the script or a link to it, as they often include extra helpful information that isn't included in the script). And if you HAVE to post the script itself for any reason, use code tags and possibly spoiler tags (for big scripts) to make them more readable.
So you want to have an enemy or several enemies chase you when you get a certain number of spaces to them?
You can do this without a script its not that time consuming either I'm assuming since your using scripts you
know the basics of rpg maker, this might seem long but when you set one up you can just copy and paste the
event for however many enemies you want

  1.     Create a new event, set it as a parallel process
  2.     Create a new Variable to check the players X position on the map, to do this go to "Insert, Control Variable, Create a new variable called Player X , go to Operand and click game data and click the ... to the right, click character and in the drop menu's choose in the left Player and in the right Map X
  3.     Create a second variable the same as above but name the new variable Player Y and choose Map Y this time
  4.     Next repeat 2 and 3 but instead of choosing player in the first game data drop box choose the event which will be the event name of your monster eg 'Monster1'
  5.     Next make a new variable "the variable will be player X, the operation will be Sub, and the operand will be the event name of the enemy (what you named number 4's variable)" in short player X - Monster X
  6.     Next repeat 5 but "player Y - monster Y"
  7.     Finally create a conditional branch, Variable player X is equal to or below 2 (or however many spaces you want its detection range to be)
  8.     repeat above but for Variable player Y
  9.     copy and paste the above 3 times so in total you'll have (player X =< 2, Player Y=< 2)(player X =< -2, Player Y=<2)(player X =< 2, Player Y=<-2)(player X =< -2, Player Y=<-2)
  10.     at the end of the conditional branch simply tell it to activate a switch called monster1 monster 2 etc then on the monster itself simply create 2 tabs one empty with random roam or whatever the second with 'Switch Monster1 is on' telling it to step towards the player and have its process be player touch activate battle etc.
This is quite basic and enemies wont stop until the player ahs left the map a little lengthy but like I said before simply copy and paste, I'm not a scripter myself and I've took it upon myself to create a game that is completely custom lol so I need to make do with what I can create with events XD probably be easier to just learn scripting lol
 

Original thread: https://forums.rpgmakerweb.com/threads/enemy-detection-on-map-and-zombie-behaviors.27030/