# Artillery game help

0 favourites
• 19 posts
From the Asset Store
25 high-quality tanks / artillery. This asset is perfect for a top-down game.
• Im trying to make an artillery game with the enemy AI firing back to the player. Im stuck at trying to get the enemy AI to fire projectiles that actually hit the player except the projectiles all fall short of the player. Any advise ?

Thanks ~

• Sounds like you need to figure out a formula for all the variables that result in a hit (probably distance from target in relation to angle, power etc). In other words, your formula should look a bit like:

If [distance from target] is X, then set [angle] to Y and [power] to Z - where Y and Z are expressed in terms of X.

And then maybe stick some random variables in so the AI doesn't score a perfect hit every time!

If you upload a capx of what you've got so far I'll have a look.

• heres my .capx. Im using turret behavior for the AI to fire at the player. Also stuck with trying to have a reload/cool down time for my player weapons instead of able to spam the shooting button.

• Yeah, the problem is that the turrent behaviour by itself is made for games with a top-down view and doesn't take things like gravity into account, while you're using a side-on view and projectiles that fall - so your enemies are trying to shoot at the most direct path to the player, but the projectiles are falling straight into the ocean

It's totally doable though - you'll just need to figure out the formula relating distance to angle, then:

Every tick > set [enemy turret angle] to [whatever angle scores a hit at that distance].

In the attached .capx, I've created a text object that displays the distance between the player and the enemy ship, and the angle of the player's turret, as well as disabling the enemy movement. If you play around with it for a bit and make some notes you should be able to get a distance/angle formula. (You might want to switch things around so that you're controlling the enemy ship's turrets and the text displays the angle of the enemy ship's turrets, since that's what you're looking for - sorry I did this in a hurry on my lunch break, there's some assembly required )

Hope this helps!

• thanks alot ! will try to work it out ! =)

• Hmmm your verison of construct 2 is r169 or newer. Mine is r168 and i cant find the latest DL from Scirra...

• Latest downloads are always shown at the bottom of the page.

• thanks dled the beta. But im not sure how to compare the distance between 2 objects and setting the turret behavior angle.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Basically, there is a relationship between the distance from one ship to another, and the angle the turret needs to be at to score a hit. It's going to take some trial and error and some maths, but you should be able to figure it out.

One place to start could be to give yourself some input tools so you can adjust the turret angle one degree at a time, and a text object showing how far the projectile has fired - eg When "Projectile" hits "Ocean" -> Set Text to int (distance(Ship.x,Ship.Y,Projectile.X,Projectile.Y)). This will show you the difference that adjusting by one degree makes. Play around with that for a while, record the results, and eventually you'll be able to say "When distance is A, angle should be B".

Once you've got that down, you can play around with things to make your game more realistic - eg, add a randomiser that increases with distance, so the enemy tends to fire less accurately at long range. The turret behaviour is great and all, but your game's a bit more complicated and it won't take physics like gravity into account for you Good luck with it!

• TBH I'd scrap the turret behaviour, you don't need it for this. Do it all in events like a pro

• how do u check and compare distance between 2 objects ?

• int(distance(Object1.x,Object1.y,Object2.x,Object2.y))

• Can you help me look at this ? Im not sure if its correct ... thanks!

• Sure, I'll take a look later

• OK, first of all holy crap that's a lot of complicated maths you had going on there under the shooting AI If you can get it to work that way that's cool, I just tried my lazy way though. After adding a "Last shot distance" to the Text object, I did some testing and noted down some relative angle/distances.

The furthest you can shoot is 503 pixels at around angle 43 (where 0's the lowest angle possible, 80 the highest). At angle 0, you shoot 124; at angle 80, you shoot 169. I noted down the distances for every 10 angles from 40-80 (since I assume you want your enemies shooting into the air, not straight forward). Then for the enemies, I just used these distances, detected the distance between the 2 ships and set the angle accordingly every 10 increments of angles (adding 185, 180 cos we're firing the other way, and another 5 to get a midpoint).

Also added the Pin behaviour to your turrets and made the enemy ship a container of them; now every time you add an enemy ship to the layout it'll come with two turrets which are linked to that particular ship, which can be handy! Added another layout called "unused", which is a good place to hold stuff that only exists in containers, otherwise it can mess stuff up on your main layout as it automatically spawns everything associated with that container

As I said, this is my really scrappy lazy way, the enemies aren't 100% accurate but I think that's more realistic You can probably improve it by figuring out the neat way; ie finding the formula for the curve of the projectiles.

Hope this helps and good luck with it, I had fun playing with it!

• 19 posts