Simon Donkers's Game Maker site siteSimon Donkers's Game Maker site
Version: GM 5+6 Views: 26192 Script, Game Maker 5 and 6 compatible
Filesize: 1499 byte Creative Commons License

Move towards instance nearest

1/52/53/54/55/5 (45 votes)

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
// www.simondonkers.com - gmmentor@simondonkers.com
//
// 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(argument0) then
  {
  if (argument[1]=0)
    argument[1]=10;
  nearest=instance_nearest(x,y,argument0)
  wanted := point_direction(x,y,nearest.x,nearest.y);
  change=min(abs(direction-wanted),argument1);
  if direction-wanted>180 then
    direction += change;
  else
    if wanted-direction>180 then
      direction += -change;
    else
      if direction-wanted<180 and direction-wanted>0 then
        direction += -change;
      else
        direction += change;
  if direction>360 then
    direction += -360;
  if direction<0 then
    direction += 360;
  }

Tip: Have a look at the tutorial How to execute a script

User comments

 Injection posted at 2006-01-16 19:51:29Quote
Nice no bugs. I'll use this in my game. :)
 ZeRo posted at 2006-11-28 13:20:35Quote
How to set a speed???
 Simon Donkers posted at 2006-11-28 18:58:16Quote
speed:=5;
:)
 mohammed-f posted at 2007-01-14 13:19:44Quote
but there is already 2 funtions for this.
one of them was : mp_linear_.... (cant remember exacetly)
 Simon Donkers posted at 2007-01-14 17:15:10Quote
This script was released before mp_linear was added, this script has no collision checking or avoiding and runs faster.
How often have you seen a heat seaking missile move through a maze?

These functions are completely different, do different things and can't be compared.
 scorpafied posted at 2007-05-17 04:21:03Quote
i put this script in my game and it comes up with errors or unknown variables please someone explain
 werewolf posted at 2007-05-23 10:10:15Quote
___________________________________________
ERROR in
action number 2
of Create Event
for object explorer:

In script homing script:
Error in code at line 8:
wanted := point_direction(x,y,nearest.x,nearest.y);

at position 42: Unknown variable x
 werewolf posted at 2007-05-23 10:28:12Quote
i dont really get this whole thing...
i dont know anything about scripting but i thought copying and pasting this into a new script file and renaming argumento to the objects name would do it. but no? what did i do wrong?
 werewolf posted at 2007-05-23 20:25:19Quote
tsch this thing dont work.. even if i do exactly everything your crappy instructions say the projectile only goes toward the enemies sometimes when they are above my character and they dont follow them... they will go towards them when they are spawned and then go strieght past them and hit the wall. boo on you.
 scorpafied posted at 2007-06-05 02:19:14Quote
i have been working on a example it shows a great simple way to make quick effective homing missiles
heres the link: http://shelbymustang500.googlepages.com/homingmissileexample.gm6
credit scorpafied if used
 Works Great ^_^ posted at 2007-06-12 04:15:57Quote
Thanks! Something that works just like it says it will for once!
 scorpafied posted at 2007-06-21 04:02:06Quote
yeah im glad you people like it!! it works better when you put it into a game surprisingly but yeah i think simon should put it on this site and give me credit for being the creator
 jonat posted at 2007-07-09 15:00:38Quote
how do you make something follow a certain object
 joe posted at 2007-10-08 21:53:57Quote
i have an idea, can you please post an example of this script, with the object names and everything put in, so we know where to put them
 joe posted at 2007-10-08 21:55:48Quote
oh crap nvm, someone already did that my bad :)
 someone posted at 2008-02-13 10:23:45Quote
it works, cool! thanks
 anonomys posted at 2008-05-31 04:43:40Quote
so can i get something that can folow an object around and never stop but not a script just events and actions for version 5.3
 anonomys posted at 2008-06-05 08:57:52Quote
How u load it into the game?
 eastley posted at 2008-07-27 04:48:34Quote
I have an error:

When I use the script, and press left, the homing object moves left, and just keeps moving left untill it hits a wall, then just sticks to the wall the whole time. How come the object won't try to chase me down?
 heromedel posted at 2008-08-03 03:06:47Quote
I got it too work just fine just rember not to use 360 because 360 will equal 0 and that means a straight line I used 359 as max also dont change the argument with no number I didnt change it because it syas agrument0 is the target and agrument1 is the max so I dont know what the non numbered agrument is so I didnt touch it and like I said it works fine for me
 newscript posted at 2009-01-14 23:04:30Quote
I'm new 2 scripts and i don't understand what to put in to make the missle chase after things. plz help.
 Daniel_84 posted at 2009-02-01 20:55:08Quote
Hey!
Thanks for the Script, i'm just starting with scripting and it's real great and simple!
 TheCakeisaLie posted at 2009-03-30 08:57:43Quote
how do i make the object move from right to left with this script????
 Simon Donkers posted at 2009-03-30 21:13:30Quote
As the title of this script suggests, it does not do that at all. Look for an entirely different script that does do what you want.
 Move towards nearest of all objects posted at 2010-02-06 17:43:22Quote
Hi, thanks for you script.
But I have a small question:
how do I make my bullet go to ANY nearest object ( not only to 1 nearest object ).
I do not understand how to do this with parents, as you suggest.
If you are in the mood, an example wood be GREAT.

Thanks :)
 Rene posted at 2010-10-01 10:57:18Quote
thnx i helped me a lot
 Rene posted at 2010-10-01 11:00:49Quote
nice nice nice. But why are all my sprites following me without the script on???
 Rene posted at 2010-10-01 11:02:14Quote
im sorry a lot. the enemys following because they HAD the script on
 Deco posted at 2010-10-01 11:12:39Quote
Thnx a lot
 tn posted at 2011-01-18 05:44:13Quote
nice post.thanks

Reply

Name:
Site: