I've trying to make enemies detect you and chase but almost every solution or tutorial I have found doesn't work
How do I make enemies detect you?
RPG Maker VXAce SupportFew years ago I did some coding for a game to implement such stuff more easily. It was made for use in RPGXP, but the adjustments to that for Ace were next to nothing.
This little script gives you the power to measure the angle and distance between two events or the angle and distance between an event and the player. Use it wisely.
The functions included are to be used within the script-option of conditional branch commands. There are several ways to use this, e.g. you could setup a watching parallel-process event that measures distance from your chaser to the player and when it is low enough you activate a switch or something to start a route of move-toward-player steps or whatever fits. Same goes for when the distance gets far enough afterwards to give up on chasing.
events_angle(8, 2).between?(90, 270)
Checks if angle between EV008 and EV002 is somewhere between 90 and 270 degrees (DEG).
player_angle(6).between?(45, 135)
Checks if angle between EV006 and the player is somewhere between 45 and 135 degrees (DEG).
player_angle == 180
Checks if angle between the event which is executing this code and the player is exactly 180 degrees (DEG).
events_distance(4, 9) < 3
Checks if radial distance of EV004 and EV009 is less than 3 tiles.
player_distance(7) > 6
Checks if radial distance of EV007 to the player is more than 6 tiles.
player_distance == 5
Checks if radial distance of the event which is executing this code to the player is exactly 5 tiles.
events_dist_round, ~_floor and ~_ceil as well as player_dist_round, ~_floor and ~_ceil are for rounding (flexible/down/up) the result of calculated distance, e.g. instead of 3.7, it would become 3 or 4.
~炬燵あ
This little script gives you the power to measure the angle and distance between two events or the angle and distance between an event and the player. Use it wisely.
Ruby:
#==============================================================================
# ** Events Angle and Distance
#------------------------------------------------------------------------------
# © 2018/2022 KotatsuAkira / AkiraKotatsuhime
#------------------------------------------------------------------------------
# Do not redistribute without copyright-notice intact.
#==============================================================================
class Game_Interpreter
#--------------------------------------------------------------------------
# * Angle between Events
#--------------------------------------------------------------------------
def events_angle(se, so)
sx = get_character(se).x - get_character(so).x
sy = get_character(se).y - get_character(so).y
return 0 if sx == 0 && sy == 0
sr = (Math.atan2(sy, sx) * 180 / Math::PI).to_i + 180
return sr.to_i % 360
end
#--------------------------------------------------------------------------
# * Angle between Event and Player
#--------------------------------------------------------------------------
def player_angle(se=0)
return events_angle(se, -1)
end
#--------------------------------------------------------------------------
# * Radial Event Distance - Original Float
#--------------------------------------------------------------------------
def events_distance(se, so)
sa = get_character(se)
sb = get_character(so)
return Math.sqrt(((sb.x - sa.x) ** 2) + ((sb.y - sa.y) ** 2))
end
#--------------------------------------------------------------------------
# * Radial Event Distance - Round Fair
#--------------------------------------------------------------------------
def events_dist_round(se, so)
return events_distance(se, so).round
end
#--------------------------------------------------------------------------
# * Radial Event Distance - Round Down
#--------------------------------------------------------------------------
def events_dist_floor(se, so)
return events_distance(se, so).floor
end
#--------------------------------------------------------------------------
# * Radial Event Distance - Round Up
#--------------------------------------------------------------------------
def events_dist_ceil(se, so)
return events_distance(se, so).ceil
end
#--------------------------------------------------------------------------
# * Distance between Event and Player
#--------------------------------------------------------------------------
def player_distance(se=0)
return events_distance(se, -1)
end
#--------------------------------------------------------------------------
# * Distance to Player - Round Fair
#--------------------------------------------------------------------------
def player_dist_round(se=0)
return player_distance(se).round
end
#--------------------------------------------------------------------------
# * Distance to Player - Round Down
#--------------------------------------------------------------------------
def player_dist_floor(se=0)
return player_distance(se).floor
end
#--------------------------------------------------------------------------
# * Distance to Player - Round Up
#--------------------------------------------------------------------------
def player_dist_ceil(se=0)
return player_distance(se).ceil
end
end
The functions included are to be used within the script-option of conditional branch commands. There are several ways to use this, e.g. you could setup a watching parallel-process event that measures distance from your chaser to the player and when it is low enough you activate a switch or something to start a route of move-toward-player steps or whatever fits. Same goes for when the distance gets far enough afterwards to give up on chasing.
events_angle(8, 2).between?(90, 270)
Checks if angle between EV008 and EV002 is somewhere between 90 and 270 degrees (DEG).
player_angle(6).between?(45, 135)
Checks if angle between EV006 and the player is somewhere between 45 and 135 degrees (DEG).
player_angle == 180
Checks if angle between the event which is executing this code and the player is exactly 180 degrees (DEG).
events_distance(4, 9) < 3
Checks if radial distance of EV004 and EV009 is less than 3 tiles.
player_distance(7) > 6
Checks if radial distance of EV007 to the player is more than 6 tiles.
player_distance == 5
Checks if radial distance of the event which is executing this code to the player is exactly 5 tiles.
events_dist_round, ~_floor and ~_ceil as well as player_dist_round, ~_floor and ~_ceil are for rounding (flexible/down/up) the result of calculated distance, e.g. instead of 3.7, it would become 3 or 4.
~炬燵あ
One of my friends made a script and some basic things with the events,
But thanks for sharing this because maybe I will be using it
But thanks for sharing this because maybe I will be using it
I don't know how else to follow up on this so, sorry for responding to a years old post... But how does this work? I've added the script and set up an event to initiate chase once a player is within distance, but it doesn't seem to work since it's activated by button press/player touch and I need it to happen from a few tiles away. Autorun/Parallel Process are forbidden in my eyes because it always causes the scene to be unplayable, though I'd totally change that if there's some trick to them aside from just "wait until the first page is finished before being able to control the player".
@Charibo While I use a different Detection Script the method is probably the same. (Vlue's Proximity Detection)
For the setup, I'd probably have a conditional branch on the first page that initiates the chase by activating selfswitchA and changing to page 2, page 2 would have a move towards player moveroute, then another 2 conditional branches the first for when the player is within 1 tile and second for if the player isn't within x tiles(for escaping chase). Both pages would be parallel processes, something like this.
Note that I've not used/read through Akira's script so I might be using it wrong. (At least this is the general setup I have for Vlue's script)
Parallel Process is the one you want to use for player detection scripts, while autorun is for cutscenes and initial setups. The whole point of parallel processes is to run code while remaining playable.
For the setup, I'd probably have a conditional branch on the first page that initiates the chase by activating selfswitchA and changing to page 2, page 2 would have a move towards player moveroute, then another 2 conditional branches the first for when the player is within 1 tile and second for if the player isn't within x tiles(for escaping chase). Both pages would be parallel processes, something like this.
Note that I've not used/read through Akira's script so I might be using it wrong. (At least this is the general setup I have for Vlue's script)
You're a legend, thanks! I don't know how I lumped the two together, but that makes so much sense! I did a quick fix and it's running exactly as intended. Now it's time to put all my unused mechanic ideas into motion with this (probably very obvious) newfound ability!
In the future, create a new thread and link to the previous ones as you've used as guides instead of necroposting and hijacking another's thread.
Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-make-enemies-detect-you.146285/