Construct 2: Enemy AI With GUNS! | Jerementor



how's it going everyone I am Jeremy Alexander and welcome to another tutorial in this tutorial we're gonna be making our enemy shoot a gun at an angle towards our players so here's how we're gonna do this we're gonna start by making global variables to do this hit V on the keyboard we're gonna make a first global variable called EXN we're gonna make one called a Y for our enemy X and enemy Y then we're gonna make one called px and py and from there we're gonna set these values we're gonna set these variables in the every check we're gonna give them the value in the average X we're going to say system set the value of x 2 equal object enemy dot X then we're gonna set the value of E Y 2 equal objects enemy dot Y we're going to keep going we're gonna say system's set the value of px to equal object player X and this is good habit because you want to store your your information in variables it's a it's good coding habit it's very popular it's very common to do another engine to set those two py to player wise so now when I actually go to type out my code I can actually just abbreviate and use these variables so it's actually even the shortcut to so what we want to do now is we want to set our position of our gun so what I have here set up is our player which is just a basic player with the platform and the scroll to behavior I have a bullet with the bullet behavior and a destroy outside the layout behavior I have a gun that has no behaviors whatsoever with the image point to the left and it has a bullet origin point at the tip of the gun there and then I have an enemy with the platform behavior and a direction instance variable so we've done a tutorial on basic enemy AI movement so the same enemy AI movement my concert she's messing up a little bit there we go so what we want to do is we want to set the position of the gun to the image point of our player or of our enemy which I didn't show you but it's there so what I'm going to do is object enemy dot image point X gun object enemy dot image point y gun now every single tick we are setting the gun to be right in the center of our player so if we look at our enemy here oh you know what and hold on I don't have that I thought I had it so right at the center of our player now this is probably just you know kind of pointless because I don't have any arms on this play or anything at all so this is kind of pointless but at the same time it's good practice since you probably are gonna have a player or enemy that is not the not a square so we're setting the gun to the position of the middle of our enemy or to wherever our arms would be then what we want to do is two things let me actually group this together call this the enemy AI and hide this because we don't need this what we want to do is we want to compare the distance and then we actually want to compare the orientation of the player of the enemy so to do this we're gonna say system compare two values we want to use the distance function now the distance function is going to get two objects and it's going to compare the distance between the two so it's kind of like using the line-of-sight behavior but it's way less performance heavy so what we're gonna do very simply is we're gonna get the enemies x position the enemy's y position the players x position and the player's y position and we're gonna find out if it's less than 100 now we're gonna mess around his number a little bit but right now we're gonna find out if it's less than 100 and if it is what we're going to do is we're gonna set the enemy angle towards px and py so we're gonna set the angle towards the player X and player Y now only if the enemy is close to or less than a hundred pixels from the player position so if it is then what we're going to do is we're going to say every 0.0 nine seconds let's have the system create the bullet object let's create the bullet object on layer zero since there's no other layers but we're gonna have it create at object enemy dot image points no sorry object Emma gun image point X bullet object enemy gun got image points why bullet okay so now it's gonna create our bullet but it's not actually going to set our angle towards our player when we're moving so to do this we're gonna set the angle of the bullet to the angle function which is going to do the same thing as the distance functions can we compare two objects and and calculate the angle between them so we're gonna say we're gonna set it to we want to do this to the enemy gun so this is something that you might want to store object enemy gun X object enemy gun Y px and py cool so now we have the angle that we want to actually store and we need to make sure that we do this here and not here because this will actually not set the motion every time a bullet is fired all right the final thing we want to do here this is pretty much the bulk of our code but we want to actually correct for the orientation of the enemy so if the player is greater then we want to find out what to do for our enemy so what we're going to say is compare the x position we're gonna find out if the enemy is greater than player X and if it is then we're gonna set the mirrored to true so we're gonna have this at mirrored and we're going to set the flip of our gun to flipped just like this then we're going to take this entire event and hit X on the keyboard and we're going to copy and paste these and we are going to reverse them so we're gonna see this work and let me make sure this is running an N wjs here let's go to the layout let's go here okay it is let me hit play and we're gonna see this so I have default controls over this and you're gonna see when I'm in when I'm a hundred pixels less then you're gonna see it working you're gonna see it flipping every time I compare to the X but when I'm away it's gonna be kind of stuck there and this is something that I wanted to show you because this is imagine there's like a circle around him around the enemy and it's only going to work when we're entering in the circle and in this case it's the circle is less than 100 pixels close to it so this is good practice and this is also just kind of good this is really just good practice is what it is but it's also just good to know how to do these kind of things now if we actually bump up our distance and if we put this to something like 500 and we hit play here then you can actually see the gun shoot every single time because it's actually calculating the distance without any problem so there we go we have the enemy shooting the gun and it's actually working out very very well because now there's no hesitation because we're not too far away so now the angle will never actually freeze and when that angle freezes it kind of gets in the way a little bit but that is all that I have for this tutorial we can obviously do a lot more to this we can add in collision objects we can add in everything else make sure you're destroying the bullet outside the layout because otherwise you're just gonna have a lot of objects even if it's even if it doesn't slow your project down you still want to make sure that you're destroying them at some point but just like this we have very simple enemy AI obviously the enemy itself can have more abilities to it so this is kind of like your action platformer basic enemy AI thank you so much for watching if you did enjoy this video please like it and leave it sums up which is the same thing as liking it leave a comment below telling me that you liked it and if you are not already subscribed please subscribe I'm doing daily videos and in addition to that I'm doing some live streams so if you want to find those out you can follow me on Twitter also I have the mega course and it's becoming a thing we're doing mega course videos a whole lot more frequently now that's getting the ball is rolling with that so I'm really excited about that we're doing 10 different games 5 for desktop 5 for mobile it's a huge course and you can get it for $25 in the description below so be sure be sure to check that out be sure to share this video thank you so much for watching and I'll see you next time

16 thoughts on “Construct 2: Enemy AI With GUNS! | Jerementor

  1. How can you have two or more enemies with guns. When i did it whenever i shot the enemy it would be completely left to chance wich one would be destroid. Any ideas on how to fix ?

  2. nice vid also check my game on my channel, I worked on it a-lot so I would appreciate any feedback.

  3. please how can i make "set dir to choose(1, -1)? that option dont appear on my construct 2 and i have the full license

  4. I did the stuff in the video and it worked fine thank you. But i want to put more than one enemy in my level, and by pressing ctrl i make copys of them but the gun only appears in the first one of them. Can you please help me, i dont know what to do? Thanks

  5. Hello i have a project with some problems, i cant do this tutorial for my game, can i send you the project for pm and solve it?, i can explain to you what i want.

  6. Thanks Jeremy. I did something similar in my current project. One enemy type (has pathfinding behavior) remains stationary until you are within x number of pixels. Once the player is within that number of pixels, I "activate" the pathfinding behavior (setting instance variable to true) while also shooting toward the players X and Y position every 1.5 sec. The enemy relentlessly chases you while shooting as long as it is in range. If you can somehow get out of range, we set that variable to false) the enemy stops in place and no more shooty shooty. Its pretty cool.

Leave a Reply

Your email address will not be published. Required fields are marked *