Calculate the angle to fire to hit enemy

Get help using Construct 2

Post » Tue Dec 11, 2012 3:19 pm

Thanks! I used a variable to make it easier on the eyes, but you could include this formula to make one long expression if you didn't want to use the variable.

Another thing - I had to refer to the bullet speed directly (the 400 in the expression) as it won't work if there are no bullet instances in the layout.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Tue Dec 11, 2012 4:15 pm

Yeah, I noticed that it wasn't working correctly for some of my bullets until I changed it to grab the bullet speed from the firing object. I'm not sure if it's some kind of bug in C2 but if you change the 400 to a very low value (e.g 1 or 2) my frame rates dropped to practically zero. Not that it mattered since my bullets aren't going to be travelling that slow, just an observation (and could be due to something else in my game, which is around 500 events long now).
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Tue Dec 11, 2012 7:14 pm

Interesting...
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Mon Jan 14, 2013 11:08 pm

NECROPOST!!!

Great example @sqiddster!

Any idea how to have it select multiple targets from the same family, allowing us to have the unit target the closest enemy, or the enemy with the lowest health?
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 931
Reputation: 9,360

Post » Mon Jan 14, 2013 11:47 pm

@AnD4D

Sure, just replace the object reference with the family, and add your extra conditions.

B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Tue Jan 15, 2013 10:02 am

@sqiddster - tried that with closest family member (sounds odd). The aiming process worked as normal, but it remained targeting the first sprite, no matter which one was closest. Should I be using a distance condition instead?

https://dl.dropbox.com/u/50465867/Construct/Targetting.capx

*SCRATCH THAT* me being stupid! Missed the positioning section at the top. Also changed it so that it rotates towards, rather than pops direction. Brilliant formula Sqiddster!AnD4D2013-01-15 10:07:23
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 931
Reputation: 9,360

Post » Tue Jan 15, 2013 11:28 am

@AnD4D Cool. I would indeed recommend a rotate towards so that it doesn't shift angle instantaneously.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Tue Jan 15, 2013 2:26 pm

I adapted the algorithm Ashley came up with here for C2

I also modified the comments for the smart aiming formula to make the explanations slightly easier to understand (although it's still complex math)
aiming.capx

Ah it ends up behing exactly equivalent to what squid came up with.

since
asin(targetSpeed * sin(angle(target,turret) - targetMovingAngle)
= asin (((targetX-turretX)*targetVectorY - (targetY-turretY)*targetVectorX)/distance(target,turret))Yann2013-01-15 14:48:05
Last edited by Yann on Sun Sep 14, 2014 11:17 am, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Jan 15, 2013 3:06 pm

Wow, Yann! That's impressive stuff! I was trying to think of ways of making sqiddster's code less accurate so the AI isn't such a brilliant shot all the time. Definately appreciated :) Thank you!
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 931
Reputation: 9,360

Post » Tue Jan 15, 2013 3:22 pm

That's some comment. My brain just melted with all the trig.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 25 guests