Please help with "Pick Nearest" and missiles.

Get help using Construct 2

Post » Fri May 24, 2013 8:35 pm

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!
B
5
Posts: 85
Reputation: 395

Post » Fri May 24, 2013 8:50 pm

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
B
34
S
16
G
16
Posts: 2,222
Reputation: 16,564

Post » Fri May 24, 2013 9:51 pm

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: http://imgur.com/aVLUkUq

B
5
Posts: 85
Reputation: 395

Post » Fri May 24, 2013 9:54 pm

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!
B
5
Posts: 85
Reputation: 395

Post » Fri May 24, 2013 10:02 pm

Hmmm ... That's an interesting issue !

Well , give me some time , I'll make you a capx ...
B
34
S
16
G
16
Posts: 2,222
Reputation: 16,564

Post » Fri May 24, 2013 11:31 pm

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 !
B
34
S
16
G
16
Posts: 2,222
Reputation: 16,564

Post » Sat May 25, 2013 12:27 am

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!
B
5
Posts: 85
Reputation: 395

Post » Sat May 25, 2013 12:37 am

Ninja'd ... ;)

Good job on that one !

You can be proud of yourself !
B
34
S
16
G
16
Posts: 2,222
Reputation: 16,564


Return to How do I....?

Who is online

Users browsing this forum: Artpunk, michaelverdi, Yahoo [Bot] and 14 guests