Game Development Reference
In-Depth Information
Homing Missiles
Practically not to be missed in any shoot-'em-up are the good old fire-and-forget homing
missiles. Rather than taking a clear aim at your target, let the missile do its own targeting using a
little bit of Game Maker logic. In this example, we'll use it to clear an asteroid field.
Start with: Reference/Framework/spaceship3.gmk
Creating Homing Missiles
1. First, open the sprites spr_missile and spr_asteroid and set the Origin to the Center
for both. Note that the origin is what we'll use to move instances and what the missiles
will be aimed at, so always make sure your targets have their origin set to the center—
you don't want missiles to aim for the top-left corner of a sprite.
2. Next, create an object called obj_missile and set the Sprite to spr_missile .
3. We will make the spaceship shoot a missile in the direction it's facing exactly like we
did in the 360-Degree Shooting example. Open obj_spaceship and add a Key Press ,
Space event. Include a Create Moving action. For Object , select obj_missile . Set X to
lengthdir_x(20,image_angle) , Y to lengthdir_y(20,image_angle) , Speed to 6 , and
Direction to image_angle . Check the Relative check box.
4. In the same event, insert a Play Sound action and set the sound to snd_fire_missile .
5. We're now able to fire a missile, but we still need it to find a target and properly track
it. Reopen obj_missile and add a Create event. Include a Set Variable action with
Variable target and Value instance_nearest(x,y,obj_asteroid) . This finds the
instance ID of the asteroid nearest to the missile and put its ID in the variable target .
We're going to use this ID to track it!
6. Next add a Step , Step event and include an Execute Code action. Insert the following lines:
1: {
2: if ( instance_exists(target) )
3: {
4: delta = point_direction(x,y,target.x,target.y)-direction;
5: if ( abs(delta) > 180 ) delta =- delta;
6: if ( abs(delta) > 4 ) direction += 4*sign(delta);
7: }
8: image_angle = direction;
9: }
It looks complicated, but all this script does is check where the target is and then
decide whether the shortest turn toward the target is clockwise or counter-clockwise,
turning at a speed of 4 degrees per step.
 
 
Search WWH ::




Custom Search