Please help with "Pick Nearest" and missiles.

0 favourites
  • 8 posts
From the Asset Store
Lasers, Blasts, Beams, Bullets, & Missiles! AAA quality Energy FX for your game.
  • Hi, I could really use some help with this one.

    Okay so I have a player spaceship which shoots missiles. The missiles chase the nearest enemy and collide and explode, killing them. This all works fine.

    The problem occurs when I want to use multiple missiles. What I want is for the first missile fired to lock on to the closest enemy, and the second missile to lock onto the next nearest enemy, and so on.

    I've given each enemy a variable which shows whether or not they are currently targeted by a missile, but all this means is that while one missile is tracking a target all subsequent missiles refuse to track anything as they all access the nearest enemy instance and find out that it is currently being targeted. I need them to check the next nearest instance, and so on.

    I hope that makes sense.

    As far as I can think, the only way to do this would be with a complicated array storing the distances of all the enemies from the player. However I really don't want to do this for two reasons:

    1. There will be a lot of enemies.

    2. I don't know much about using arrays in C2.

    If anyone could help me with a good method of doing this, or point me in the right direction to start with Arrays, that would be awesome!

    Thanks!

  • On missile created ;

    (Enemy) pick nearest to missile

    = Set missile Variable1 to enemy uid

    Every tick ;

    If missile Variable1=Enemy.Uid

    = move toward Enemy

    Easy , and doesn't need an array

  • Thanks Whiteclaws! But I don't think that gives the effect I'm looking for.

    This code *seems* the same as I had before, and it means that when multiple missiles are fired at once, they will all track the same enemy. I need them all to track different enemies, so if I fire five missiles at the same time, then they will individually track the five closest enemies.

    Here's a screenshot of my missile event sheet: imgur.com/aVLUkUq

  • Oh, and is it possible to add a delay to the "on created" command. So, when an object is created it will wait 1 second before executing a piece of code?

    I want to do this so that the missiles spray out in random directions before acquiring their targets.

    Cheers!

  • Hmmm ... That's an interesting issue !

    Well , give me some time , I'll make you a capx ...

  • Meh ... Don't have a lot of time ,

    But that's how I would go about it !

    An Array

    Enemy ( Variables : LockUID , Move toward enemy UID depending on variable )

    Missile

    For Each Missile

    -- (Ordered) For Each Enemy depending on distance(Missile.X,Missile.Y,Enemy.X,Enemy.Y)

    --- (invert) Enemy.UID is in array

    ---- Push UID in Array

    ---- Set LockUID to Enemy.UID       

    Here we go ... Hope that helped !

  • SWISH.

    I have invented Santh's Schizophrenic Seeker-Swarmers

    On Created

    Pick nearest Enemy to (Player.X, Player.Y)

    IF Enemy.Targeted = 0

    [Set Missile.Target to Enemy.UID]

    [Set Enemy.Targeted to 1]

    ELSE

    WHILE

    Missile.Target = 0

    Pick a random Enemy instance

    IF Enemy.targeted = 0

    [Set Missile.Target to Enemy.UID]

    [Set Enemy.Targeted to 1]

    This means that the missiles will target the closest enemy, unless it has already been targeted, in which case they will target a random untargeted enemy.

    It's a pretty decent effect. Especially when fired in bursts. Still needs some fine tuning though.

    Thanks for your help!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ninja'd ... ;)

    Good job on that one !

    You can be proud of yourself !

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