Homing Bullets.

0 favourites
  • 12 posts
From the Asset Store
Hundreds of environmental props and variations for creating awesome home interiors
  • Hello guys! I am quite new here and managed to build a little space shooter by myself and some Youtube help but now I'm kinda stuck :(

    I want to create a bullet that bounces to a random target nearby and then destroys itself.

    I tried "Move to" and "Turret" but nothing seems to work. Do I need 1 or 2 bullets and what behaviours do they need?

    I hope someone can help me. Thanks in advance! <3

    Andi

  • I want to create a bullet that bounces to a random target nearby and then destroys itself.

    Edit: A bullet that bounces from an enemy to another random enemy nearby and then destroys itself.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Firstly you will need to keep track if any given bullet can bounce, or if it should destroy itself. You can do this with an instance variable on the bullet object. Lets call it bounce, and set it to 1 by default.

    On collision with target, add some sub events. You can compare instance variables. If bullet.bounce is 0, destroy bullet. If bullet.bounce is > 0, subtract 1 from bounce, pick a new target based on your conditions, and set bullet angle of motion to angle(bullet.x,bullet.y,target.x,target.y).

  • Thanks! I will give feedback if it worked out!

  • Okay so the bouncing works but the way the bullet picks a new enemy doesn't.

    The ricochet bullet won't travel towards a random enemy. Most of the time it flies of the screen in a random direction. The bullet has to fly towards an enemy that is on screen but it feels like it flies towards something that is not on screen... I don't know.

    Because I have 4 different enemies I tried this:

    choose(angle(Bullet_8.x,Bullet_8.y,Enemy_1.x,Enemy_1.y),angle(Bullet_8.x,Bullet_8.y,Enemy_2.x,Enemy_2.y),angle(Bullet_8.x,Bullet_8.y,Enemy_3.x,Enemy_3.y),angle(Bullet_8.x,Bullet_8.y,Enemy_4.x,Enemy_4.y))

    So the bouncing bullet will choose between enemy 1, 2, 3 and 4. But this doesn't work either.

    What am I missing? I feel like I am very close to a solution but the bullet just won't behave like I want it to.

    :(

  • If you have 4 separate sprite objects as "enemies", you would normally put them all into a "enemies" family, and then you can use the system - pick random instance to get a random one. That way you don't have to repeat your events for each enemy object.

  • Thanks for the advice, I wasn't aware of the family option.

    But still: How to make the bullet work properly? When it hits an enemy, the enemy explodes and the bullet will keep on flying. So far so good.

    But it flies completely random, sometimes to the right, sometimes down and sometimes it even hits enemies (I think this is pure luck though).

    I need the bullet to only hit on-screen enemies.

    By the way: How can I make the bullet target a random enemy from the enemy family using "system - pick random instance"? What do I have to do after picking?

    I'm very sorry but I'm still not familiar with Construct so my questions may seem stupid to you.

  • After narrowing down a target by picking, you can use the action set angle of motion to angle(bullet.x,bullet.y,family.x,family.y), where bullet is the bullet's current location, and family is the x and y of the picked object. The angle() expression gives the angle between the two.

    You can also narrow down the targets by using the "Is on screen" condition before picking a random instance. Note the order is important, conditions filter in order. So if you used pick random instance, and it picked something off screen, and then had "is on screen" afterwards, you would end up with nothing picked.

  • Is this the correct way? I mean it kinda works now but it sure looks like a mess for you.

    The only problem that's left is: the bullet only hits it's target after setting the speed of the picked random enemy to zero (as seen in the image) because the bullet flies to the location where the enemy just was.

    Do you know a workaround for this?

    Thanks oosyrag for the help so far!

  • If you want homing bullets, you'll need to set a target for it, and then set the bullet angle of motion towards that target every tick.

    You'll also want some additional instance variables, so that each bullet can "remember" what it's target was.

    Or projecting the trajectory of the target is also an option, but that can get even more messy.

  • Here's an example, there were a few things I missed like picking all enemies again after colliding with one of them (normally only the one that triggered the collision is picked. We want to pick all so that we can randomly pick a new target after that.

    I used the enemy's UID to store as a target for a bullet.

    dropbox.com/s/7n3u84okx4ifeej/homingbullets.c3p

  • So in case you want to know: I managed to do exact what I wanted!

    A bullet that travels upwards and after it hits an enemy the bullet becomes homing towards another random enemy.

    Thank you oosyrag, you were a great help!

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