This is a post about doing homing missiles in GameMaker!
Theory
This example applies principles from "angular rotations explained".
To be more specific, once you get the "angle rotation" part down, getting an object to gradually rotate towards target takes one line of code.
Scripts
First, the utility scripts. These depend on your GameMaker version:
GM≤8.1
You will need the cycle
and angle_rotate
scripts from the aforementioned post,
/// 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;
GM:Studio and GMS≤2.2.5
GM:S introduced an angle_difference
function,
which allows angle_rotate
to be simplified.
/// angle_rotate(angle, target, speed) var _angle = argument0, _target = argument1, _speed = argument2; return _angle + clamp(angle_difference(_target, _angle), -_speed, _speed);
GMS≥2.3 and GM2022+
Same deal, but using the new function syntax.
function angle_rotate(_angle, _target, _speed) { return _angle + clamp(angle_difference(_target, _angle), -_speed, _speed); }
Use
With scripts added to your project, adjusting trajectory is a matter of
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 someobj_player
or an instance ID) -
3
is the rotation speed, in degrees per frame.
Add acceleration (speed = min(speed + accel, maxspeed)
)
and image_angle = direction
if needed,
and there you have it - a missile.
Demo
Here's a little live demo:
And here's the 2013 example that you can see on the GIF in the beginning of this post:
Revisions
Date | Changes |
---|---|
Jun 04, 2013 | Initial publication |
Mar 19, 2017 | Fixed download links, added a live demo |
Oct 04, 2024 | Added snippets for different GameMaker versions, revised text |
Have fun!
How might you make this so a missile can turn while stationary?
Nevermind, figured it out.
Thank you so much!!!
That’s amazing!
Thank you for sharing!
Is there a way to make this go around walls and such, instead of being destroyed on contact with them?
Your safest bet would probably be to use mp_potential_step, which (via mp_potential_settings) similarly rotates the object while following, but will attempt to avoid obstacles.
You’ve ended my endless search!!
1000 thank-you’s.
I savor, lead to I discovered exactly what I was taking a look for.
You’ve ended my 4 day long hunt! God Bless you man. Have a nice day.
Bye
Why 4 days mate? Do you not know gm? Perhaps you should look into the functions instead of CPing code. Copying people’s example is gonna do you know good if you don’t know what you are doing. Hopefully you tried to at least make a homing missle.
Anyways, I’m only saying this because it held my younger self back quite a bit in the beginnings of my gamemaker experience.
It seems that you have a simple mistake in your demo .gmk file. When a rocket collides with a block, it tries to create “obj_gib”, but in your object resource list, there is no “obj_gib”. Instead there is an object called “obj_shard”, and switching the code from trying to create an instance of “obj_gib” to “obj_shard” resulted in the error being fixed.
Otherwise, good example! I believe some may find it useful.
Thanks, I’ve fixed that.
Was a post-publication edit because term “gib” doesn’t quite apply to fragments of mechanical objects. Didn’t pay attention to whether I saved last change or not.