# Artillery game help

Get help using Construct 2

### » Mon May 19, 2014 10:50 am

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 ~
B
9
S
2
Posts: 31
Reputation: 635

### » Mon May 19, 2014 10:59 am

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.
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

### » Mon May 19, 2014 11:20 am

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.
You do not have the required permissions to view the files attached to this post.
B
9
S
2
Posts: 31
Reputation: 635

### » Mon May 19, 2014 12:03 pm

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!
You do not have the required permissions to view the files attached to this post.
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

### » Mon May 19, 2014 11:15 pm

thanks alot ! will try to work it out ! =)
B
9
S
2
Posts: 31
Reputation: 635

### » Mon May 19, 2014 11:18 pm

Hmmm your verison of construct 2 is r169 or newer. Mine is r168 and i cant find the latest DL from Scirra...
B
9
S
2
Posts: 31
Reputation: 635

### » Tue May 20, 2014 6:35 am

If your vision so exceeds your ability, then look to something closer.
Moderator
B
145
S
33
G
89
Posts: 5,914
Reputation: 62,593

### » Wed May 21, 2014 2:50 am

thanks dled the beta. But im not sure how to compare the distance between 2 objects and setting the turret behavior angle.
B
9
S
2
Posts: 31
Reputation: 635

### » Wed May 21, 2014 3:29 pm

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!
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

### » Wed May 21, 2014 4:00 pm

TBH I'd scrap the turret behaviour, you don't need it for this. Do it all in events like a pro
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

Next