Hi. Are there any free plugin that make enemy chase player when they close to them?
MZ Enemy Chasing Player
JS Plugin RequestsYou can do this without a plugin with
$gameMap._events[this._eventId].moveStraight($gameMap._events[this._eventId].findDirectionTo( $gamePlayer.x,$gamePlayer.y ));
If you want it only to chase you if you're close use
If: Math.sqrt(($gamePlayer.x - this.character(0).x) ** 2 + ($gamePlayer.y - this.character(0).y) ** 2) <= 5
Replace 5 with the distance you want.
$gameMap._events[this._eventId].moveStraight($gameMap._events[this._eventId].findDirectionTo( $gamePlayer.x,$gamePlayer.y ));
If you want it only to chase you if you're close use
If: Math.sqrt(($gamePlayer.x - this.character(0).x) ** 2 + ($gamePlayer.y - this.character(0).y) ** 2) <= 5
Replace 5 with the distance you want.
Thank you! But, where can i put the code?
I edited my post to modified the code above a bit. Put it in the event page and put a wait of at least 1 frame because this is pathfinding so it's processor heavy.
It's worked. But, i have new problem. Look like enemy is teleport to me, Are there anyway to fix it?
You can change the move frequency, speed or number of wait frames.
Thank you so much!!
Ah, i have one more question. How do i start a battle when enemy catched player? Because it is parallel, it will automatic start a battle when the conditions are met (Player is in enemy's range but not touch the enemy yet)
Nvm, i solved it. Thank you!
Then make another if statement with
Math.sqrt(($gamePlayer.x - this.character(0).x) ** 2 + ($gamePlayer.y - this.character(0).y) ** 2) <1
And put Battle Processing under it
Adding to this, based on my understanding of RMMV, if you want the event to keep chasing you even after beating it or fleeing from it, then you may want to add a Wait to it for after Battle Processing has ended to give the player a chance to flee.
Alternatively, if the player wins, you may want to utilize Erase Event. The event will reappear in its default position the next time the player appears on that map.
If you don't want the enemy to exist ever again, you can set a Self-Switch where when flipped, the enemy is gone.
Someone who knows better than I can confirm if this is smart or not. It's just how I understand it so far.
Alternatively, if the player wins, you may want to utilize Erase Event. The event will reappear in its default position the next time the player appears on that map.
If you don't want the enemy to exist ever again, you can set a Self-Switch where when flipped, the enemy is gone.
Someone who knows better than I can confirm if this is smart or not. It's just how I understand it so far.
I have a question, where should i put the statement?
Original thread: https://forums.rpgmakerweb.com/threads/enemy-chasing-player.160037/