# Problem with Enemy AI?

**URL:** <https://phaser.discourse.group/t/problem-with-enemy-ai/6860>\
**Category:** Phaser 3\
**Created:** [July 4, 2020, 5:24pm UTC](https://phaser.discourse.group/t/problem-with-enemy-ai/6860 "2020-07-04T17:24:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [July 4, 2020, 5:24pm UTC](https://phaser.discourse.group/t/problem-with-enemy-ai/6860/1 "2020-07-04T17:24:29Z")

</div>

So I’m trying to get my enemy to chase the character unless it gets too close, in which case the enemy starts attacking, if the player gets too far away, the enemy goes back to its’ waypoint.

What am I doing wrong here?

Why won’t my enemy chase me correctly?

```javascript
// If the `enemy` is touching the `ground`

if ( isOnFloor ( entity.body ) === true ) {

    // Get the `distance` `from` `player`

    var distFrom = ( player.sprite.width );
    
    // Get the `distance` `to` `enemy`

    var distTo = ( entity.width );

    // Set a `minimum` range

    var minDistRange = ( 5 );

    // Set a `maximum` range

    var maxDistRange = ( 7 );

    // Get this `distance` between the 
    // `player` & enemy

    var dist = ( distFrom - distTo );

    // Get the `minimum` distance based on `width` of `enemy`
    // plus the `minimum` range of distance

    var minDist = ( ( dist ) + ( minDistRange ) );

    // Get the `maximum` distance based on `width` of `enemy`
    // plus the `maximum` range of distance

    var maxDist = ( ( dist ) + ( maxDistRange ) );

    // Get the non-negative `minimum` distance based on `width` of `enemy`

    var absMinDist = ( Math.abs ( minDist ) );

    // Get the non-negative `maximum` distance based on `width` of `enemy`

    var absMaxDist = ( Math.abs ( maxDist ) );

    // If the `Player` is within range of the `enemy`

    if ( absMinDist > minDistRange && absMaxDist < maxDistRange ) {
        
        // Chase the `Player`

        // Set the `velocity` of the `enemy` dependent on whether 
        // positive or negative & multiply by 
        // `enemy` minimum movespeed

        entity.setVelocityX ( Math.sign ( absMinDist ) * ENEMY_MIN_MOVESPEED );

        // Set `enemy` Animation to `walk`

        PlayAnimation ({
            sprite : entity, 
            animName : 'walk', 
            loopAnim : true, 
        });

    }

    // Otherwise, the `Player` is NOT within the range 
    // of the `enemy`

    else {

        // Stop `enemy` movement

        entity.setVelocity ( 0 );

        // Set `enemy` Animation back to `idle`

        PlayAnimation ({
            sprite : entity, 
            animName : 'idle', 
            loopAnim : true, 
        });

    }

}

```

ANY & ALL help is GREATLY appreciated!

Thank You! \<3

---

<div class="post-metadata">

**Author:** ![Darko](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/darko/32/3335_2.png) [@Darko](https://phaser.discourse.group/u/Darko)\
**Post date:** [July 5, 2020, 9:18am UTC](https://phaser.discourse.group/t/problem-with-enemy-ai/6860/2 "2020-07-05T09:18:32Z")

</div>

Hello, just an idea here…  
calc the distance with the **position** using `x` and/or `y` of the player and enemy, instead of using `width`?

---

<div class="post-metadata">

**Author:** ![Marcio\_Ramos](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/marcio_ramos/32/744_2.png) [@Marcio\_Ramos](https://phaser.discourse.group/u/Marcio_Ramos)\
**Post date:** [July 12, 2020, 12:18am UTC](https://phaser.discourse.group/t/problem-with-enemy-ai/6860/3 "2020-07-12T00:18:04Z")

</div>

Wherever enemy or player will be, their width will be the same.  
You should verify their coordinates x,y instead.
