Game Development Reference
In-Depth Information
Line 4 sets up a variable
delta
that contains the difference in angle between the
direction the missile is currently going in and the direction it needs to turn into to face
the target, and we use
point_direction
to get this angle. If it's positive, it means the
missile must turn counter-clockwise (increase its angle of direction); if it's negative,
the missile must turn clockwise (decrease its angle of direction).
Line 5 checks if the difference is bigger than a 180-degree turn (the function
abs
always returns a positive value, which helps us in our
if
statement). If it is, we invert
delta
because it will be quicker to turn the other way!
Line 6 changes the direction with 4 degrees multiplied by the sign of
delta
: if
delta
is negative,
direction
is lowered by 4; otherwise, it is increased by 4. We only do this,
however, if the difference is greater than 4, otherwise our missile will wiggle toward the
targeted direction. The final line adjusts the image's angle to the direction.
7.
That's all we need to track it. Now for some destructive power, add a
Collision
event
with
obj_asteroid
and include a
Create Effect
action (
draw
tab) with
Type
firework
,
Size
small
, and
Where
set to
above objects
, and check the
Relative
box.
8.
Include a
Play Sound
action and set
Sound
to
snd_explosion
.
9.
In the same event, include two
Destroy Instance
actions, one that
Applies to
Self
and
one to the
Other
.
10.
Finally, include an
Other
,
Outside Room
event and include a
Wrap Screen
action,
in
both directions.
Result:
Reference/Result/homing_missiles1.gmk
Try out a few shots, and notice how our missile never fails to meet its target—unless another
asteroid gets in the way! It even turns around if the asteroid wraps to the other side of the screen,
and if its target was destroyed by another missile, it just keeps flying straight until it hits
something. You'll notice that its effectiveness is dependent on the speed with which we allow it to
make turns—the slower we let it turn, the longer it takes before it is back on track toward its
target. Now it's up to you to invent all kinds of variations on the theme—do you want it to set a
different target if its target is destroyed? Or should it self-destruct immediately or after a given
amount of time? One cool variation is to make the missile heat-seeking, targeting anything that is
within a certain range.
Changing the Missile to Heat-Seeking the Nearest Object
1.
Open
obj_missile
and delete the
Create
event.
2.
Select the
Step
event and open the
Execute Code
action. At line 2, insert the following
two lines:
target = instance_nearest(x,y,obj_asteroid);
if ( distance_to_object(target) > 100 ) target = noone;
Instead of setting a target only once in the
Create
event, this changes target to the
nearest instance every step. But if the target is more than 100 pixels away, we set it to
noone
—which means that the missile will just fly straight without changing direction.
Result:
Reference/Result/homing_missiles2.gmk
Search WWH ::
Custom Search