Hey everyone, I'm trying to find a simple way to get distance between a player and an event. I am using a proximity event plugin, but it doesn't allow anything for if that the player leaves that proximity. I know how to load play and event map/screen locations in variables, but I can't figure out how to use them, so I think seeing it done would help alot.
So the just of this is I need an event to run some code (stop playing music, go back to event location) when I get so far away from it. I'm ok with scripts, plugins or even just eventing. Thanks in advance!
Trying to find a distance between Player and Event
● ARCHIVED · READ-ONLY
-
-
Tutorials is where people who have made tutorials can post them
[move]RPGMaker MV[/move] -
Probably there is a plugin to do that, but you can use this script call to store the distance between player(x,y) and event(x,y) into a variable:
Code:var player = { 'x':$gamePlayer.x, 'y':$gamePlayer.y, }; var event = { 'x':$gameMap.event(1).x, 'y':$gameMap.event(1).y }; var dist_y = player.y - event.y; var dist_x = player.x - event.x; var dist = Math.sqrt(dist_x*dist_x + dist_y*dist_y); $gameVariables.setValue(15,dist);
$gamemap.event(1) -> 1 is the event ID
$gameVariables.setValue(15,dist); -> 15 is the variable that stores the distance.
You can use it on a parallel evento to constantly check the distance.
Distance is returned as a number with decimals, diagonal distances are sometimes returned as numbers like 2,83748374
Example parallel event to switch to night screen tint when player is more than 6 cells away from event 1.
-
you don't even need a script, all that data is directly available through control variable - game data (and then the map coordinates for event or player)
-
you don't even need a script, all that data is directly available through control variable - game data (and then the map coordinates for event or player)
Andar, yes but I think sqrt is not avaiable as a math operation on variables,
you need at least a "set variable = Script" to do that. -
Thanks, I didn't actually know where this should belong.Tutorials is where people who have made tutorials can post them
[move]RPGMaker MV[/move] -
Raw example:
Var1 = Player map x
Var1 - Event map x
Conditional Branch Var 1 less than 0 ? if yes Var1 * (-1)
Var2 = Player map y
Var2 - Event map y
Conditional Branch Var 2 less than 0 ? if yes Var2 * (-1)
Var1 + Var2 (= Distance of Event to Player, Diamond Shape )
Edit: Here an usage example, its just one possible Methode, but there are different ones, depending on the wanted outcome.
If Var1 is equal or more than 0
If Var1 is less than 3
If Var3 is not equal to 1 (With this Condition you execute the following Code just one time when needed. if its permanently executed, that could cause Performance Problems or the Music would start over and over all the time)
Yes: Set Var3 = 1
Play BGM River Max Volume
If Var1 is equal or more than 4
If Var1 is less than 6
If Var3 is not equal to 2
Yes: Set Var3 = 2
Play BGM River Medium Volume
If Var1 is equal or more than 7
If Var3 is not equal to 3
Yes: Set Var3 = 3
Stop BGM River
The Closer you come towards the Event, the louder the River Music or Sound would Play. The further away you get, the more it would fade off. -
Great, I'm going to fool around with this and get back to you. Appreciate the response.Raw example:
Var1 = Player map x
Var1 - Event map x
Conditional Branch Var 1 less than 0 ? if yes Var1 * (-1)
Var2 = Player map y
Var2 - Event map y
Conditional Branch Var 2 less than 0 ? if yes Var2 * (-1)
Var1 + Var2 (= Distance of Event to Player, Diamond Shape )
Edit: Here an usage example, its just one possible Methode, but there are different ones, depending on the wanted outcome.
If Var1 is equal or more than 0
If Var1 is less than 3
If Var3 is not equal to 1 (With this Condition you execute the following Code just one time when needed. if its permanently executed, that could cause Performance Problems or the Music would start over and over all the time)
Yes: Set Var3 = 1
Play BGM River Max Volume
If Var1 is equal or more than 4
If Var1 is less than 6
If Var3 is not equal to 2
Yes: Set Var3 = 2
Play BGM River Medium Volume
If Var1 is equal or more than 7
If Var3 is not equal to 3
Yes: Set Var3 = 3
Stop BGM River
The Closer you come towards the Event, the louder the River Music or Sound would Play. The further away you get, the more it would fade off. -
I made a mistake in the hurry.
If Var1 is less than 3
it Needs to be
If var 1 is equal or less than 3.
same for the block with the number 6.
Edit:
If you want to use the Scriptline in Event Commmands, there is a 3rd possible solution.
I remember @caethyril mentioning it in the past.
$gameMap.distance(x1, y1, x2, y2)
and the Code to use would look like this for variable1:
$gameVariables.setValue(1, $gameMap.distance($gamePlayer.x, $gamePlayer.y, $gameMap.event(1).x, $gameMap.event(1).y) )
or this with direct coordinates:
$gameVariables.setValue(1, $gameMap.distance($gamePlayer.x, $gamePlayer.y, 8, 15) )