make object move towards player p5js 1

make object move towards player p5js

this.rotation = atan2(player.y - this.y, player.x - this.y);
this.speed = 2.5;

this.x += cos(this.rotation) * this.speed;
this.y += sin(this.rotation) * this.speed;

Here is what the above code is Doing:
1. We’re creating a variable called rotation and setting it equal to the angle between the player and the enemy.
2. We’re creating a variable called speed and setting it equal to 2.5.
3. We’re adding the cosine of the rotation to the x position of the enemy.
4. We’re adding the sine of the rotation to the y position of the enemy.

Similar Posts