# Homing missile

Get help using Construct 2

### » Sat Mar 17, 2012 7:37 pm

Hello

I am in the process of porting my game from Game Maker to C2 and all has been smooth so far except for this issue

I need the player to shoot an object which will lock on to the nearest enemy of a specific type and then move towards it until they collide

So basically i need to
1. Find the nearest enemy
2. Make sure the missile follows it
3. Make the missile continue in a straight line if that enemy is destroyed before it hits its target

Apologies if this is already covered somewhere but i looked through the FAQ and did a search in this section but didn't find anything i could use

Any help will be much appreciated
B
15
S
3
G
1
Posts: 91
Reputation: 1,664

### » Sat Mar 17, 2012 8:29 pm

If you are using the bullet behavior, or move at angle.
1: Bullet pick closest target
2: Bullet set angle towards target
3: Wont matter if 1 and 2 are continuous.
B
173
S
50
G
189
Posts: 8,515
Reputation: 118,764

### » Sat Mar 17, 2012 8:37 pm

[QUOTE=newt] If you are using the bullet behavior, or move at angle.
1: Bullet pick closest target
2: Bullet set angle towards target
3: Wont matter if 1 and 2 are continuous.[/QUOTE]

I don't understand

I am using bullet behaviour but i don't see anything about pick closest target in it's behaviour
I am hopeless at trigonometry so i was hoping i could do this some other way. Like in Game Maker there is a move towards behaviour you put in the every tick event

What am i missing?

jn2002dk2012-03-17 20:39:59
B
15
S
3
G
1
Posts: 91
Reputation: 1,664

### » Sat Mar 17, 2012 9:54 pm

Last edited by Yann on Sun Sep 14, 2014 11:17 am, edited 1 time in total.
B
68
S
22
G
14
Posts: 1,486
Reputation: 16,565

### » Sat Mar 17, 2012 10:01 pm

[QUOTE=Yann] http://dl.dropbox.com/u/23551572/C2/homingMissiles.capx[/QUOTE]
You are awesome!

That's exactly what i need

Thank you, thank you, thank you
B
15
S
3
G
1
Posts: 91
Reputation: 1,664

### » Mon Mar 19, 2012 5:39 pm

Thanks Yann, great work! you're excellent.
B
33
S
5
G
4
Posts: 7
Reputation: 7,743

### » Mon Mar 19, 2012 11:08 pm

If you want the missiles to behave a bit more realistically, I would suggest, instead of setting the angle straight towards the object, every tick set the angle to: anglelerp(rocket.angle,angle(rocket.x,rocket.y,target.x,target.y),dt*somespeedfactor)
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,729

### » Wed Mar 21, 2012 10:13 am

[QUOTE=sqiddster] If you want the missiles to behave a bit more realistically, I would suggest, instead of setting the angle straight towards the object, every tick set the angle to: anglelerp(rocket.angle,angle(rocket.x,rocket.y,target.x,target.y),dt*somespeedfactor)[/QUOTE]
Will try this

I can't claim to have a firm grasp of what lerp does exactly but it can't hurt to try. Thank you:)
B
15
S
3
G
1
Posts: 91
Reputation: 1,664

### » Wed May 22, 2013 3:01 pm

Turret + Bullet works AWESOME for me.
HARMLESS SIGNATURE THAT IS VIEWED BY OTHERS AS OFFENSIVE AND CONTROVERSIAL
B
14
S
3
G
3
Posts: 356
Reputation: 4,377

### » Wed May 22, 2013 4:20 pm

@fassFlash:

Turret didn't exist a year ago when these posts were posted.
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Next