Simon Donkers
Game Maker
Home > GameMaker > Scripts > Move towards instance nearest

Move towards instance nearest

A script which let the object move towards the nearest instance with a small change of direction per step. Useful for a heat seaking missile.

 Move towards instance nearest
//////////////// MOVE TOWARDS NEAREST ////////////////////
// Copyright Simon Donkers 3-7-2006
// -
// argument0 is object name
// argument1 is max change or cource per turn. (optional)
// Use 360 to make it move in a straight line
// when using multiple objects select one parent object.
// Note: set the speed yourself!
var nearest,change,wanted;
if instance_exists(argument0then
  if (argument[1]=0)
  wanted := point_direction(x,y,nearest.x,nearest.y);
  if direction-wanted>180 then
    direction += change;
    if wanted-direction>180 then
      direction += -change;
      if direction-wanted<180 and direction-wanted>0 then
        direction += -change;
        direction += change;
  if direction>360 then
    direction += -360;
  if direction<0 then
    direction += 360;