Enemies Spot your position

Hi, do you know if there's a way, using only scripts and without the help of plugins, to make an enemy wander around the map, so that when spot the player or the player is close, the enemy runs toward it? Because if I just set it to follow the player, it will follow them all over the map no matter what, i need something like in Fear and hunger kinda, Thank you very much in advance!
... , using only scripts and without the help of plugins, ...
This distinction is mostly pointless. A plugin is just a compile-on-boot script packaged in a convenient unit. A script call (i.e. eval, compiles & runs on the spot) is less efficient. Chase logic can potentially get quite complicated, that's why people write plugins for it. 😅

That said:
Here's my test example, works if pasted into a single Script command in an event's auto move route:
Code:
var chaseDist = 8, baseSpeed = 3, chaseSpeed = 5, baseFreq = 4, chaseFreq = 5, wanderChance = 20; var inRange = $gameMap.distance(this.x, this.y, $gamePlayer.x, $gamePlayer.y) < chaseDist; if (inRange) { this.setMoveSpeed(chaseSpeed); this.setMoveFrequency(chaseFreq); } else { this.setMoveSpeed(baseSpeed); this.setMoveFrequency(baseFreq); }; if (inRange && Math.randomInt(100) < (100 - wanderChance)) { this.moveStraight(this.findDirectionTo($gamePlayer.x, $gamePlayer.y)); } else { this.moveRandom(); }
Yep, that's a lot of code! 👋 However, notice there's a collection of values defined at the start...that's so you don't have to dig through the actual code hunting them down, you can just change the starting values and everything should adjust correctly. 😂 Here's what each one should do:
  • chaseDist: limits how many tiles away the event should start chasing from;
  • baseSpeed: movement speed when not chasing the player (player walk speed = 4, dash speed = 5);
  • chaseSpeed: movement speed while chasing;
  • baseFreq: movement frequency when not chasing the player;
  • chaseFreq: movement frequency when chasing;
  • wanderChance: % chance of random movement while in range for chasing;
Feel free to make any edits you like to the script! 🙏
I posted it for MV nearly 7 years ago, and haven't tested it in MZ, but I think it should work. It only does what it says, e.g. it doesn't make the character return to their original position post-chase. 👋

If you want something more complicated, like line-of-sight, then I recommend looking for a suitable plugin. In theory I think it should be possible to implement a line-raster algorithm without scripting, and use that to check region IDs on an arbitrary sight-line, but I would personally choose a plugin.
This distinction is mostly pointless. A plugin is just a compile-on-boot script packaged in a convenient unit. A script call (i.e. eval, compiles & runs on the spot) is less efficient. Chase logic can potentially get quite complicated, that's why people write plugins for it. 😅

That said:

I posted it for MV nearly 7 years ago, and haven't tested it in MZ, but I think it should work. It only does what it says, e.g. it doesn't make the character return to their original position post-chase. 👋

If you want something more complicated, like line-of-sight, then I recommend looking for a suitable plugin. In theory I think it should be possible to implement a line-raster algorithm without scripting, and use that to check region IDs on an arbitrary sight-line, but I would personally choose a plugin.
Thank you so much, I didn't want to use Plugins because it's a simple game and I really couldn't understand them since English isn't my native language and most tutorials are in English or other languages. your script works just fine ty!

Original thread: https://forums.rpgmakerweb.com/threads/enemies-spot-your-position.176188/