# How to make a Heat Seeking missile?

12 replies to this topic

### #1 MMMStudios

MMMStudios

GMC Member

• New Member
• 136 posts

Posted 25 February 2012 - 05:21 PM

I wanted to know how to make a Heat Seeking missile for my Dive Man (MegaMan) game. I certainly know how to make it move towards an object, but I want it to curve. For example, if Dive Man was shooting on the opposite direction of the nearest object, I don't want it to just move directly to it, I want it in a curve (Not really good at describing; sorry). Thanks for all help given, I'm still getting use to GML coding, because I don't think your able to do this in D&D.

Thanks!
• 0

### #2 shledder

shledder

GMC Member

• GMC Member
• 96 posts
• Version:GM8

Posted 25 February 2012 - 06:19 PM

I would suggest setting the speed of the missile to be constant, or constantly increasing. Set an alarm for how ever "smart" you want the missile to be. When the alarm times out, call a script which checks to see if the direction of the missile is the direction it needs to be going to hit (you'll need some trigonometry and vectors here): if so, do nothing; if not direction += x where x is how fast you want the missile to be able to turn.

Hope this gets you started
• 0

### #3 fluidic ice

fluidic ice

GMC Member

• GMC Member
• 812 posts
• Version:GM:Studio

Posted 25 February 2012 - 06:22 PM

(you'll need some trigonometry and vectors here)

You wouldn't really need trig or vectors to do this, the code below will curve a missile around to a target slowly to provide a curving feel. To make it curve even more, you could randomize a point to pass through on it's way to the target, like a detour which would increase the curve, or another way would be to initially shoot the missile at an angle that's not in the direction of the target to get it to curve more.

```CREATE:
Turn_Speed = 2; //How fast the missile turns.

STEP:
Target_Direction = point_direction(x,y,Obj_Player.x,Obj_Player.y); //The target the missile will go towards.
diff = Target_Direction - image_angle;
while (diff > 180) diff -= 360;
while (diff < -180) diff += 360;
if (abs(diff) < Turn_Speed)
{
image_angle = Target_Direction;
}
else
{
image_angle += (sign(diff) * Turn_Speed);
}
direction = image_angle;```

Edited by fluidic ice, 25 February 2012 - 06:28 PM.

• 0

Try out my game Point of Contact: Here (New Tab)

Or check out my music! Here

### #4 GMRescue

GMRescue

GMC Member

• GMC Member
• 125 posts
• Version:GM8

Posted 26 February 2012 - 12:01 PM

Dear fluidic ice,

A heatseeking missile requires simple GML coding.

If the missile seeks obj_man

If your player is called obj_man (you can modify this name) and you want the player to chase obj_man, than this code might be fitting:

In the step event:

`move_towards_point(obj_man.x,obj_man.y,(speed that you want))`

Speed that you want might be: 4, 5 or 6.

If there is more than one player

If there are more players and the missile chases the nearest one:

Step event:

`move_towards_point(instance_nearest(x,y,obj_man).x,instance_nearest(x,y,obj_man).y,(speed that you want))`

It's not that hard, but if you have any questions, PM me or reply to this.

- GMRescue
• 0

Game Maker. I started with GM5 Lite. I still remember that. It's already been years. Game Maker has encountered a lot of improvement and developement. From the very first Game Maker to GM Studio Pro. - I'm still working with GM8 Pro and I cannot imagine using an other version (anymore).

### #5 fluidic ice

fluidic ice

GMC Member

• GMC Member
• 812 posts
• Version:GM:Studio

Posted 26 February 2012 - 12:44 PM

Dear fluidic ice,

A heatseeking missile requires simple GML coding.
- GMRescue

Huh? I wasn't the one asking how to do it if that's what you thought?
I was just showing the code I use to get an object to curve around it it's target. =p
• 0

Try out my game Point of Contact: Here (New Tab)

Or check out my music! Here

### #6 GMRescue

GMRescue

GMC Member

• GMC Member
• 125 posts
• Version:GM8

Posted 27 February 2012 - 06:04 AM

Dear fluidic ice,

A heatseeking missile requires simple GML coding.
- GMRescue

Huh? I wasn't the one asking how to do it if that's what you thought?
I was just showing the code I use to get an object to curve around it it's target. =p

Ok, you can watch a tutorial on YouTube.
• 0

Game Maker. I started with GM5 Lite. I still remember that. It's already been years. Game Maker has encountered a lot of improvement and developement. From the very first Game Maker to GM Studio Pro. - I'm still working with GM8 Pro and I cannot imagine using an other version (anymore).

### #7 greep

greep

Menaces with Spikes

• GMC Member
• 2398 posts
• Version:GM7

Posted 27 February 2012 - 06:38 AM

Fluidic's code is the one that is needed, though. Your missile is 100% accurate, the OP wants something that can take a while to correct it's direction.

Edited by greep, 27 February 2012 - 06:42 AM.

• 0

Spoiler

### #8 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 27 February 2012 - 01:01 PM

Slightly smaller version...
```if (instance_exists(myTarget)) {
image_angle=direction;
}```
myTarget should be assigned to an instance id, and the code shown above should be put into the step event.

rotSpeed is a variable relating to how fast the missile can turn.
• 0

Did I help you? If so, help me by pressing the + button above here.

### #9 MMMStudios

MMMStudios

GMC Member

• New Member
• 136 posts

Posted 28 February 2012 - 02:28 AM

Dear fluidic ice,

A heatseeking missile requires simple GML coding.

If the missile seeks obj_man

If your player is called obj_man (you can modify this name) and you want the player to chase obj_man, than this code might be fitting:

In the step event:

`move_towards_point(obj_man.x,obj_man.y,(speed that you want))`

Speed that you want might be: 4, 5 or 6.

If there is more than one player

If there are more players and the missile chases the nearest one:

Step event:

`move_towards_point(instance_nearest(x,y,obj_man).x,instance_nearest(x,y,obj_man).y,(speed that you want))`

It's not that hard, but if you have any questions, PM me or reply to this.

- GMRescue

Well, I was asking for one that can curve.

Anyways, I really like the method you gave Fluid Ice. I forgot to state though, that I wanted the missile to also do a loop when the player shoots it from the other side and it aims towards an object behind him, instead of just flipping straight to the object. (Hopefully I'm descriptive enough.)
• 0

### #10 fluidic ice

fluidic ice

GMC Member

• GMC Member
• 812 posts
• Version:GM:Studio

Posted 28 February 2012 - 02:58 AM

Do you mean if the player has their back to the target? If so the missile should already do a 180 over time and come back to hit the target?
If not are you able to draw a quick sketch in paint about what sort of movement you were going for?
• 0

Try out my game Point of Contact: Here (New Tab)

Or check out my music! Here

### #11 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 28 February 2012 - 03:48 AM

Did you even read my code?

Yes, it does the same as fluidic ice's, but it's shorter and more efficient.
• 0

Did I help you? If so, help me by pressing the + button above here.

### #12 MMMStudios

MMMStudios

GMC Member

• New Member
• 136 posts

Posted 28 February 2012 - 07:58 PM

Did you even read my code?

Yes, it does the same as fluidic ice's, but it's shorter and more efficient.

I read your code, but I already saw Fluid Ice's code so I didn't really see why I should take time to switch codes just for efficiency.

Anyways, yes Fluid Ice that's exactly what I want the player to be able to do. If he has his back to the object I want the missile to do a 180 loop instead of just flipping.
• 0

### #13 itsosmpl

itsosmpl

GMC Member

• New Member
• 7 posts
• Version:GM8

Posted 02 May 2012 - 09:34 PM

Slightly smaller version...

```if (instance_exists(myTarget)) {
image_angle=direction;
}```
myTarget should be assigned to an instance id, and the code shown above should be put into the step event.

rotSpeed is a variable relating to how fast the missile can turn.

i used this one and it works perfect, thanks allot!
• 0