Inception92

Regular
Regular
Joined
Nov 20, 2013
Messages
47
Reaction score
8
First Language
English
Primarily Uses
RMMZ
One thing I was hoping for assistance with (don't know if this is possible with RPG maker as it is, or if I'd require a plugin or whatever), is having enemy encounters that are triggered by you the player being detected by them.

A basic example would be like what you would expect of Pokemon games, where if you're within a specified distance in front of an event you'll trigger the event, them going and engaging you.

What I would prefer though is something I've seen in some other games made with rpg maker (such as Omori), where if you get too close to an enemy they'll chase after you, with it being possible to outrun them, with the enemy going back to normal, walking around at random.
 

ZombieKidzRule

I’m back from my wilderness Snorlax retreat!
Regular
Joined
Jan 9, 2022
Messages
1,205
Reaction score
2,479
First Language
English
Primarily Uses
RMMZ
Yes, this is possible.

I have several tutorials about proximity triggers and there are also YouTube videos dealing specifically with Line of Sight. In fact, The Odie has several I believe.

Just search YouTube for RPG Maker MZ Line of Sight and you will immediately find a few options.

I hope that helps!
 

werzaque

Canned Dinosaur
Regular
Joined
May 30, 2023
Messages
436
Reaction score
263
First Language
EN/JP/NL
Primarily Uses
RMMZ
Not wanting to bother with plugins, I have put a scripted movement route in a normal, trigger-on-touch event. The scripted movement will just switch between "move at random" or "chase the player" based on the distance between the event and the player. Like so:

JavaScript:
if(Math.abs($gamePlayer.x - $gameMap.event(this._eventId).x) + Math.abs($gamePlayer.y - $gameMap.event(this._eventId).y) < 6){
this.moveStraight(this.findDirectionTo($gamePlayer.x,$gamePlayer.y));$gameMap.event(this._eventId).setMoveSpeed(4);
} else {
$gameMap.event(this._eventId).moveRandom();$gameMap.event(this._eventId).setMoveSpeed(1);
}

If you want to include line-of-sight (as in, the enemy won't notice you when approaching from behind), it's slightly little more work I guess. Note that you can replace the entire "$gameMap.event(this._eventId)" by "this" and it will work too.
 
Last edited:

Inception92

Regular
Regular
Joined
Nov 20, 2013
Messages
47
Reaction score
8
First Language
English
Primarily Uses
RMMZ
I remember coming across one video on line of sight around a year ago, and even though I did and copied everything the guy in the video did, it wasn't working right.

I just remember the guy showing off 4 different types of LoS with scripts, and only one or two of the types he showed off worked. As for the others, they would trigger no matter how far away from the specified distance I was.

The way I managed to get some kind of perimeter effect with an earlier section of my game was by using this one plugin called "MOG - Event Sensor" by Moghunter. It's a pretty janky plugin though, and isn't line of sight at all. Also I have to setup multiple tabs in the event editor to make the plugin carry out whatever I want the event itself to do.
 

werzaque

Canned Dinosaur
Regular
Joined
May 30, 2023
Messages
436
Reaction score
263
First Language
EN/JP/NL
Primarily Uses
RMMZ
Can you elaborate what your requirements are? I've had the same experience with code lying around, and it wasn't always clear whether it was a MV/MZ compatibility issue or I was not implementing it correctly, hence me abandoning that and trying it myself.

1. what distance (how many tiles) do you want the enemy to notice you?
2. is direction that the enemy is facing important? (i.e. does the enemy need to SEE the player)
3. what is the change in behavior that you want when the enemy notices you?

Am I assuming correctly that this is for MZ?
 

Inception92

Regular
Regular
Joined
Nov 20, 2013
Messages
47
Reaction score
8
First Language
English
Primarily Uses
RMMZ
Can you elaborate what your requirements are? I've had the same experience with code lying around, and it wasn't always clear whether it was a MV/MZ compatibility issue or I was not implementing it correctly, hence me abandoning that and trying it myself.

1. what distance (how many tiles) do you want the enemy to notice you?
2. is direction that the enemy is facing important? (i.e. does the enemy need to SEE the player)
3. what is the change in behavior that you want when the enemy notices you?

Am I assuming correctly that this is for MZ?
I wouldn't mind having sections in game where stealth can be used to avoid altercations with enemies. Basically being able to sneak around enemies (preferably ones that are moving, and not stationary). Another thing I would also like to do (isn't a necessity though) is instead of having random encounters triggered by steps, is to have the creatures/enemies be on the map itself, so you have more control over if you want to engage them or not, vs you being stopped in your tracks and forced to deal with an encounter every x amount of steps.

1. It doesn't need to be a huge distance. Hard to give a definitive answer on this since it will always depend on the map design, but I'd say less than 10 tiles.
2. Obviously I want the enemy to see the player. If you're within 6 or so tiles and they're facing you I feel they should react to you being there.
3. Basically to chase after you and trigger a battle processing if they touch you.

Also I put MZ in the title.
 

werzaque

Canned Dinosaur
Regular
Joined
May 30, 2023
Messages
436
Reaction score
263
First Language
EN/JP/NL
Primarily Uses
RMMZ
Okay, first the version ignoring line of sight, so basically the enemy "smells" you. Set the route of the event to custom, trigger on event touch, and put this in the route as a script (it's stripped of all linefeeds otherwise there's no way to paste it in the small field):

JavaScript:
const Pla = $gamePlayer;const DivX = Pla.x - this.x;const DivY = Pla.y - this.y;const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));if(Dist < 7){this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));this.setMoveSpeed(4);} else {this.moveRandom();this.setMoveSpeed(1);console.log(Dist)}


The "unminified" version of the same just so that it's easier to follow:
JavaScript:
//Advanced Zombie 2
const Pla = $gamePlayer;
const DivX = Pla.x - this.x;
const DivY = Pla.y - this.y;
const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));
if(Dist < 7){
    this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));
    this.setMoveSpeed(4);
    } else {
        this.moveRandom();
        this.setMoveSpeed(1);
        }
 

Inception92

Regular
Regular
Joined
Nov 20, 2013
Messages
47
Reaction score
8
First Language
English
Primarily Uses
RMMZ
Okay, first the version ignoring line of sight, so basically the enemy "smells" you. Set the route of the event to custom, trigger on event touch, and put this in the route as a script (it's stripped of all linefeeds otherwise there's no way to paste it in the small field):

JavaScript:
const Pla = $gamePlayer;const DivX = Pla.x - this.x;const DivY = Pla.y - this.y;const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));if(Dist < 7){this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));this.setMoveSpeed(4);} else {this.moveRandom();this.setMoveSpeed(1);console.log(Dist)}


The "unminified" version of the same just so that it's easier to follow:
JavaScript:
//Advanced Zombie 2
const Pla = $gamePlayer;
const DivX = Pla.x - this.x;
const DivY = Pla.y - this.y;
const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));
if(Dist < 7){
    this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));
    this.setMoveSpeed(4);
    } else {
        this.moveRandom();
        this.setMoveSpeed(1);
        }

So after watching some videos, I was eventually able to figure out how to get line of sight working (using this one guys guide https://theodie.wixsite.com/rmtut/line-of-sight-page )

As for this here, I played around with this and do have it working, with the event chasing me just like how I want, with it going back to doing its own thing when I run away.

The only hiccup though is when it comes to being chased, if I go to diagonal directions, then the model kind of glitches out and also stops moving.
Screenshot_6.png
 

werzaque

Canned Dinosaur
Regular
Joined
May 30, 2023
Messages
436
Reaction score
263
First Language
EN/JP/NL
Primarily Uses
RMMZ
You mean to say this occurs how? The code

JavaScript:
this.moveStraight(this.findDirectionTo($gamePlayer.x,$gamePlayer.y))

should work whether the gameplayer is moving diagonally or not, since it will only try to move ONE STEP in ONE OF THE FOUR DIRECTIONS the player is relative to the event. I'm curious as to how this can happen. Could you elaborate on what you did?
 
Last edited:

Inception92

Regular
Regular
Joined
Nov 20, 2013
Messages
47
Reaction score
8
First Language
English
Primarily Uses
RMMZ
You mean to say this occurs how? The code

JavaScript:
this.moveStraight(this.findDirectionTo(gamePlayer.x,gamePlayer.y))

should work whether the gameplayer is moving diagonally or not, since it will only try to move ONE STEP in ONE OF THE FOUR DIRECTIONS the player is relative to the event. I'm curious as to how this can happen. Could you elaborate on what you did?
Just moving in circles around the event while it followed me. The sprites would randomly start spazzing out, for example the legs and waist could be on the upper half of the event sprite and the top half would be down a long the bottom of the event. And if I stop at certain directions around the event it'd just freeze in place.
 

werzaque

Canned Dinosaur
Regular
Joined
May 30, 2023
Messages
436
Reaction score
263
First Language
EN/JP/NL
Primarily Uses
RMMZ
Hmmm... that is curious. Does the console (F12) say anything noteworthy?
 

ClockworkFigure

Regular
Regular
Joined
Jul 7, 2022
Messages
40
Reaction score
27
First Language
English
Primarily Uses
RMMZ
Visustella's Encounter Effects plugin should do a great job for you if you just want something working out of the box for this specific purpose.
 

Inception92

Regular
Regular
Joined
Nov 20, 2013
Messages
47
Reaction score
8
First Language
English
Primarily Uses
RMMZ
Hmmm... that is curious. Does the console (F12) say anything noteworthy?
Not as far as I can tell.

Visustella's Encounter Effects plugin should do a great job for you if you just want something working out of the box for this specific purpose.
Will definitely look into this.
 

Inception92

Regular
Regular
Joined
Nov 20, 2013
Messages
47
Reaction score
8
First Language
English
Primarily Uses
RMMZ
Hmmm... that is curious. Does the console (F12) say anything noteworthy?
After messing around with things, I was finally able to resolve the problem. I tried using that Encounter Effects plugin, but was running into the same problems, with the images for the event glitching out, and the event getting stuck depending on where I stood. So I assumed that it was likely a diagonal movement plugin that was causing the problem.

I use a plugin (Galv's) to allow for diagonal movement. I don't have it enabled for my characters to use diagonal image sets, but I think what was happening was with the script I was using the events themselves would behave like they have diagonal images, even though their set has no, which is why their images were shuffling around like that.

With Galv's plugin, one thing you can enable/disable is the ability to move diagonal when you use your mouse to move, with the window for the plugin displaying a message that says "true may conflict with pathfinding plugins". I disabled that feature, and when I tested out your script the event moved and behaved as it should, and wasn't coming to a stop anymore, and also wasn't glitching out with its image.
 

werzaque

Canned Dinosaur
Regular
Joined
May 30, 2023
Messages
436
Reaction score
263
First Language
EN/JP/NL
Primarily Uses
RMMZ
Thanks for testing, much appreciated! That explains why I couldn't reproduce your finding.

I created a prototype accounting for line of site. It's basically applied to the move route script as with the earlier one, but it's made of two parts. By splitting it in two parts (a trick I learned from caethyril), I've attempted to separate the "start chasing" trigger from the "stop chasing" trigger.

I suppose that at this point it's easier to use one of the plugins lying around, but it was a fun exercise in my rudimentary javascript!

JavaScript:
//I See Live People Zombie 4 Part 1
const Pla = $gamePlayer;
const DivX = Pla.x - this.x;
const DivY = Pla.y - this.y;
const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));
const Face = this.direction();
if(!this.ISeeLivePeople){
    if(Dist < 7 && ((Face === 2 && DivY > 0)||(Face === 4 && DivX < 0)||(Face === 6 && DivX > 0)||(Face === 8 && DivY < 0))){
        this.ISeeLivePeople = true;
        $gameTemp.requestBalloon(this,4);
        this._waitCount = 59;
    } else {
        this.moveRandom();
        this.setMoveSpeed(1);
    }
}
//I See Live People Zombie 4 Part 2
const Pla = $gamePlayer;
const DivX = Pla.x - this.x;
const DivY = Pla.y - this.y;
const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));
const Face = this.direction();
if(this.ISeeLivePeople) {
    this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));
    this.setMoveSpeed(4);
    if(Dist > 4 && !(Dist < 7 && ((Face === 2 && DivY > 0)||(Face === 4 && DivX < 0)||(Face === 6 && DivX > 0)||(Face === 8 && DivY < 0)))){
        $gameTemp.requestBalloon(this,2);
        this._waitCount = 59;
        delete this.ISeeLivePeople
    }  
}
 
Last edited:

ClockworkFigure

Regular
Regular
Joined
Jul 7, 2022
Messages
40
Reaction score
27
First Language
English
Primarily Uses
RMMZ
.. I tried using that Encounter Effects plugin, but was running into the same problems, with the images for the event glitching out, and the event getting stuck depending on where I stood. So I assumed that it was likely a diagonal movement plugin that was causing the problem... (etc.)

I use a plugin (Galv's) to allow for diagonal movement.. (etc.)


Interesting. Yes I've used a number of Galv plugins too. What you're saying would make sense. If you went ahead and bought Encounter Effects, you might want to have a look at the Events and Movement Core wiki page and read up on their 8 direction movement. To fully integrate it visually, you also need to make a special "VS8" sprite sheet for your sprite which is different from the normal format. It takes a moment to get your head around, but straightforward once you do.

If you're abandoning this solution, that's cool too, but that's my three cents having toiled and tinkered a lot with Visustella and Galv. PS I forgot how much Encounter Effects was capable of! Cool plugin.
 

Inception92

Regular
Regular
Joined
Nov 20, 2013
Messages
47
Reaction score
8
First Language
English
Primarily Uses
RMMZ
Thanks for testing, much appreciated! That explains why I couldn't reproduce your finding.

I created a prototype accounting for line of site. It's basically applied to the move route script as with the earlier one, but it's made of two parts. By splitting it in two parts (a trick I learned from caethyril I was able to create something.), I've attempted to separate the "start chasing" trigger from the "stop chasing" trigger.

I suppose that at this point it's easier to use one of the plugins lying around, but it was a fun exercise in my rudimentary javascript!

JavaScript:
//I See Live People Zombie 4 Part 1
const Pla = $gamePlayer;
const DivX = Pla.x - this.x;
const DivY = Pla.y - this.y;
const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));
const Face = this.direction();
if(!this.ISeeLivePeople){
    if(Dist < 7 && ((Face === 2 && DivY > 0)||(Face === 4 && DivX < 0)||(Face === 6 && DivX > 0)||(Face === 8 && DivY < 0))){
        this.ISeeLivePeople = true;
        $gameTemp.requestBalloon(this,4);
        this._waitCount = 59;
    } else {
        this.moveRandom();
        this.setMoveSpeed(1);
    }
}
//I See Live People Zombie 4 Part 2
const Pla = $gamePlayer;
const DivX = Pla.x - this.x;
const DivY = Pla.y - this.y;
const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));
const Face = this.direction();
if(this.ISeeLivePeople) {
    this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));
    this.setMoveSpeed(4);
    if(Dist > 4 && !(Dist < 7 && ((Face === 2 && DivY > 0)||(Face === 4 && DivX < 0)||(Face === 6 && DivX > 0)||(Face === 8 && DivY < 0)))){
        $gameTemp.requestBalloon(this,2);
        this._waitCount = 59;
        delete this.ISeeLivePeople
    } 
}
With that it's not letting me paste the script, with only the first line being pasted
Nvm, I was able to figure it out and get it working. I think I like this one more than the first one you did for me.
JavaScript:
const Pla = $gamePlayer;const DivX = Pla.x - this.x;const DivY = Pla.y - this.y;const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));const Face = this.direction();if(!this.ISeeLivePeople){    if(Dist < 7 && ((Face === 2 && DivY > 0)||(Face === 4 && DivX < 0)||(Face === 6 && DivX > 0)||(Face === 8 && DivY < 0))){        this.ISeeLivePeople = true;        $gameTemp.requestBalloon(this,4);        this._waitCount = 59;    } else {        this.moveRandom();        this.setMoveSpeed(1);    }}
JavaScript:
const Pla = $gamePlayer;const DivX = Pla.x - this.x;const DivY = Pla.y - this.y;const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));const Face = this.direction();if(this.ISeeLivePeople) {    this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));    this.setMoveSpeed(4);    if(Dist > 4 && !(Dist < 7 && ((Face === 2 && DivY > 0)||(Face === 4 && DivX < 0)||(Face === 6 && DivX > 0)||(Face === 8 && DivY < 0)))){        $gameTemp.requestBalloon(this,2);        this._waitCount = 59;        delete this.ISeeLivePeople    }   }
 
Last edited:

werzaque

Canned Dinosaur
Regular
Joined
May 30, 2023
Messages
436
Reaction score
263
First Language
EN/JP/NL
Primarily Uses
RMMZ
Yes, you need to "minify" the script by removing all the linefeeds and tabs, like so:

JavaScript:
//I See Live People Zombie 4 Minified Part 1
const Pla = $gamePlayer;const DivX = Pla.x - this.x;const DivY = Pla.y - this.y;const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));const Face = this.direction();if(!this.ISeeLivePeople){if(Dist < 7 && ((Face === 2 && DivY > 0)||(Face === 4 && DivX < 0)||(Face === 6 && DivX > 0)||(Face === 8 && DivY < 0))){this.ISeeLivePeople = true;$gameTemp.requestBalloon(this,4);this._waitCount = 59;} else {this.moveRandom();this.setMoveSpeed(1);}}
//I See Live People Zombie 4 Minified Part 2
const Pla = $gamePlayer;const DivX = Pla.x - this.x;const DivY = Pla.y - this.y;const Dist = Math.round(Math.sqrt(DivX ** 2 + DivY **2));const Face = this.direction();if(this.ISeeLivePeople) {this.moveStraight(this.findDirectionTo(Pla.x,Pla.y));this.setMoveSpeed(4);if(Dist > 4 && !(Dist < 7 && ((Face === 2 && DivY > 0)||(Face === 4 && DivX < 0)||(Face === 6 && DivX > 0)||(Face === 8 && DivY < 0)))){$gameTemp.requestBalloon(this,2);this._waitCount = 59;delete this.ISeeLivePeople}}
 
Last edited:

werzaque

Canned Dinosaur
Regular
Joined
May 30, 2023
Messages
436
Reaction score
263
First Language
EN/JP/NL
Primarily Uses
RMMZ
Nvm, I was able to figure it out and get it working. I think I like this one more than the first one you did for me.
Thanks, I'm actually moderately satisfied with the results myself. Next step would be to make this more... user-friendly. Would be nice to turn it into a plug-in/function call, in which one can specify the detection distance, walking speed and walking frequency upon detection, balloon upon detection, action taken (chase or jump), etc... etc...
 

Inception92

Regular
Regular
Joined
Nov 20, 2013
Messages
47
Reaction score
8
First Language
English
Primarily Uses
RMMZ
Thanks, I'm actually moderately satisfied with the results myself. Next step would be to make this more... user-friendly. Would be nice to turn it into a plug-in/function call, in which one can specify the detection distance, walking speed and walking frequency upon detection, balloon upon detection, action taken (chase or jump), etc... etc...
I'm able to figure out which one to change for distance, as well as the ones for the balloons
 

Latest Threads

Latest Profile Posts

Yknow what? Im seriously considering recruiting a manager to oversee my games development.
Because I cannot focus or complete these tasks by myself. I need someone to give me orders, without having them be my boss.
yp_4vS.png

Remember my latest plugin for rpg maker mz:

Acknowledgement Window is now available!

Take a look here:

Got my focus back, 9/59 maps have the door fix in place now.
Making a small RMMV project has made me realize that I've never actually made a credits sequence for a game.

Forum statistics

Threads
136,799
Messages
1,270,153
Members
180,553
Latest member
agadh
Top