(mouseover/click to play GIF)

This is a post about implementing homing missiles in GameMaker.

A homing missile is a type of missile that can alter its direction mid-flight to hit a (generally) moving target.

This example shares much of the concept with "angular rotations explained".

First, you will need cycle and angle_rotate scripts from there,

/// cycle(value, min, max) var result , delta ; delta = ( argument2 - argument1 ); result = ( argument0 - argument1 ) mod delta ; if ( result < 0 ) result += delta ; return result + argument1 ;

/// angle_rotate(angle, target, speed) var diff ; // 180 is to be replaced by "pi" for radians diff = cycle ( argument1 - argument0 , - 180 , 180 ); // clamp rotations by speed: if ( diff < - argument2 ) return argument0 - argument2 ; if ( diff > argument2 ) return argument0 + argument2 ; // if difference within speed, rotation's done: return argument1 ;

The rest is trivial - you want to find the target' coordinates and have your missile rotate in that direction, like so (in Step event),

if ( instance_exists ( target )) { direction = angle_rotate ( direction , point_direction ( x , y , target . x , target . y ), 3 ); }

Where target is a variable holding the object to track (be that some obj_player or an instance ID), and 3 is rotation speed, in degrees per frame.

Add acceleration ( speed = min(speed + accel, maxspeed) ) and image_angle = direction , if needed.

And that's it. Here's a small live demo if you'd like:

The downloadable example (shown in GIF above) expands on this slightly, adding the ability to place/remove walls, and spotting simplistic visuals akin to one of my older games:

Download GMK

Usage instructions for the example:

Add obj_launcher, obj_rocket, obj_shard to your source. obj_shard only needed for rocket explosions - if you're going to make your own, you can omit adding that and remove relevant code from "obj_rocket:collision with obj_wall" event). Change occurrences of obj_target in obj_rocket and obj_launcher to name if your desired target object (for most games this is probably your player object). If you have multiple target objects in room, you may need to add target determination code (such as instance_nearest(x, y, obj_player) ) there. Tweak parameters (third parameter in angle_rotate calls and speed modifiers in obj_rocket ) to suit your game mechanics.

Have fun!

Note: this post was originally published on Jun 4, 2013. It was updated on Mar 19, 2017, adding a live demo and important bits of the source code to the post.

Related posts: