Here in this lecture, we'll add horizontal movement to the enemies. Basically, they should be able to move horizontally towards the player to make the game more challenging. So let's get to it. For this of course, I'll open Oh enemy, I'll open the step event and go to its very bottom. Now before moving the enemy towards the player, we want to check if the player instance exists in the room. So for that I'll search exists, I need this instance as a distraction.
I'll add it after the last condition and not inside it. Now this action simply checks if an instance exists inside the room. So I click here and select all player. You can also see the condition in the action overview. Now if the player instance does exist, we want the enemy to move towards it, but only on the x axis. So I'll search for and add the variable action.
I'll attach it to our condition. Now here I'll create a variable called diff. This will show the difference between the players x and the enemies x We are going to use this value to make the enemy move towards the player. So that's why the players x comes first in the subtraction. So if I add this value to the enemy's x, it will move immediately to the players x. So to move it there, gradually we'll add the value sign to the x, the sign of a number tells whether that number is positive, negative or zero.
So if it's positive, the sign is one, if it's negative, the sign is minus one. And if it's zero, the sign is zero as well. So we get the enemy's existence to the player and reduce it to either one or minus one. This value will then be added to the x so that the enemy moves towards the player. So I'll add another variable action for adding to the x so I'll enter x here and now in the value I'll enter sign and inside the brackets are entered if the sign here is a function and we are passing the value as the first argument. So the function We'll return the sign of that value which will then be added to the apps and now are on the game are played.
And you can see that the enemies Now move towards the player. So this one was easy. Now let's move on to the next lecture we will add more player shapes