0 Favourites

Calculate the angle to fire to hit enemy

  • I've read up on various topics on this but they all seem to include strange mathematical symbols and are coded in other languages. Can anybody explain in plain English (or a Capx) how this is achieved?

    If I have a stationary object which needs to fire at a moving object (both seen from directly above), how do I calculate the angle to fire the bullet at? I know the speed of the bullet and the enemy is moving at a constant speed.

  • Have an invisible sprite that is constantly attached to the shooting sprite, and also setting its angle towards the target every tick. Then when you need to shoot, just get that object to create the bullet and its angle will be automatically set the same as the invisible sprite, which is in turn pointed directly at the target.

  • This is the method I use:

    Bullet: on created > Set angle: *angle(bullet.x, bullet.y, enemy.x, enemy.y)*

    When setting the angle, just copy-and-paste the section between the *s, then change the names from bullet and enemy to whatever you have named your bullet and enemy.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Sorry, I didn't describe it properly. What I meant was, the object is moving so the bullet needs to be fired ahead of the object, otherwise the object moves along it's course and the bullet drops behind. 'Deflection shooting' I think they call it. I need to calculate where to fire the bullet ahead of the object so that they'll collide.

  • It looks like you'll have to understand that "complicated math" if you want a complicated prediction. It's like throwing a basketball in a moving hoop: you see how fast it's going, predict where it will be, and throw.

    In terms of C2, a simple solution would be to create an empty sprite where the object will be, angle the bullet towards it, and fire. If the bullet is somewhat slow, then speed complicates the matter further.

    Good luck getting a working sample.

  • There's a fairly simple solution which isn't perfect, which is to work out the distance between the firing object and the target object, divide that by 6 or 7 (will vary depending on the bullet speed), then add that distance infront of the object in the angle it's travelling, and fire towards that point. That does give fairly decent results but it's not perfect and only works with set bullet speeds.

    I'll have to go back and work out what the hell a quaternion is and how to translate that to C2.

  • Great, you got me all mathexcited. Unfortunately I don't have time right now to work out the equations for you, but I'll see what I can do later ;)

  • Science!

    It works rather well, if I may say so ;)

  • Great Sample

  • Wow, excellent stuff, and it's all contained on a single line! Now that'll save me a lot of headaches, thanks :)

  • 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.

  • 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).

  • Interesting...


    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?

  • AnD4D

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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)