Having enemy walking toward player

Hello, and have a nice day.

I would have a difficulty with a small game in top down.
I would like the player to be able to be followed by enemies that will constantly walk in his direction, but I can’t manage to do it and I can’t really find directions, so I would like to ask for your help.

I found a phaser tutorial on github and another one on the internet that talks about a method


update(targetOfCrab) {
        //#region movement
        const speed = 300;

        this.body.setVelocity(0);

                this.rotation = Phaser.Math.Angle.Between(this.x, this.y, targetOfCrab.x, targetOfCrab.y)
                this.physics.velocityFromRotation(this.rotation, speed, this.body.velocity);

    } // End of update


but I get an error message and I don’t understand where it could come from…

Do you know why I’m having this one ? Thanks a lot for the time and have a great day !

I assume the this reference in the Crab.js update method is not what you’re expecting it to be. this in the scene object refers to the scene which has properties like physics. I assume you call Crab update in your GameScene update method. If so, you could try calling it like this Crab.update.call(this, target) where target refers to the player or whatever.

1 Like