Posts > Building Game with Phaser: Enemy Range Detection
June 16, 2026
In a Zelda game, some enemies are triggered when Link comes near on the range of enemy. Either they attack with arrows or move to where Link is and attack. I want to try this to my game, but mine is just a simple one, I wanted to make enemy move to the players place and do an attack.
This method checks for the pixels distance of object 1 and object 2.
const distance = Phaser.Math.Distance.BetweenPoints('obj_1', 'obj_2');I’m using BetweenPoints here because I just want to know points between the two object. There’s a lot of other type of function you can use. ( check the Phaser official documentation )
moveToObjectMove the enemy object to specific destination or in our case the player object.
this.physics.moveToObject(this.enemy, this.player);
// moveToObject(gameObject, destination, [speed], [maxTime])With that in mind what we need now is animation for your enemy
update(){
const distance = Phaser.Math.Distance.BetweenPoints(enemyObj, playerObj);
if ( distance < 100){
this.physics.moveToObject(enemyObj, playerObj);
this.anims.play('enemy-walk-animation', true);
} else {
enemyObj.body.setVelocityX(0);
this.anims.play('enemy-idle-animation', true);
}
}So what I’m doing is when the distance between the two object ( player and enemy ) is 100px the enemy object will move next to the player object.
This is post is just the gist of it, there’s a lot of other scenario that you can think of from this, like adding attacking functionality when enemy is beside or near the player. This is just the basic way to make a enemy or object move to a certain destination.