GameMaker: Studio

GameMaker: Studio

View Stats:
HeadcrabPL Apr 22, 2014 @ 11:27pm
How to make object move to nearest enemies
I found alot of ways to to move to nearest object, but not to move to one of multiple ones.
I have 3 objects: Civilian, Soldier, Zombie.
What code do i need to use to make zombies run to civilians or soldiers depending on wich one is closer
< >
Showing 1-4 of 4 comments
Blyxx Apr 23, 2014 @ 6:50am 
Define an object that is the parent of both civilians and soldiers, and have the zombies run to the nearest instance of that object.
Zaron X Apr 23, 2014 @ 10:16am 
The parent object route works, as does using a couple with() statements, though that could get rather slow depending on how many instances are calling it or being looked at by it. Another potential solution would be to spawn an object that follows its creator that zombies zero in on, though in effect this is basically the same as the parent/child thing and adds another object to hog resources in the process.

Another method could be to find the nearest instance of each type of object, and then determine the nearest of those found. Theoretically, something like this should work:

//Temp variables var nHuman; var nSoldier; var nCivilian; //I'm also going to say there's a local variable called Target. Yay! //Find the nearest instance of each target type. nHuman = instance_nearest(x, y, obj_Player); nSoldier = instance_nearest(x, y, obj_Soldier); nCivilian = instance_nearest(x, y, obj_Civilian); /*TODO: It may be wise to make sure an instance of any given type of object exists before running the checks for which is closest. If your game has a controller object, it could work out to simply leave that outside of the room and, if the returned value for any of our nWhatever variables equals noone, set their value to the controller's ID. This would almost always result in the controller being the farthest away, and stop any value retrieval errors caused by an instance not existing. Another option would be to always default to the Player object if nothing is found, but you'll need a condition that comes first below for if the Player object itself does not exist, or you risk similar conflicts.*/ //Figure out which potential target is closest if distance_to_point(nHuman.x, nHuman.y) <= distance_to_point(nSoldier.x, nSoldier.y) and distance_to_point(nHuman.x, nHuman.y) <= distance_to_point(nCivilian.x, nCivilian.y) then { //Player is closest. Target = nPlayer; //If player is not closest, check between the other two. } else if distance_to_point(nSoldier.x, nSoldier.y) <= distance_to_point(nCivilian.x, nCivilian.y) then { //Soldier is closest. Target = nSoldier; } else { //By process of elimination, the Civilian is the closest. Target = nCivilian; } //At this point, you probably want to have the zombie move towards the Target.

To keep zombies from turning crazy erratic, you may want to have them retarget on a timer, or even become obsessed with a target until it's either defeated or a great distance away. Both behaviors are easy enough to implement, but neither is necessary.

Hope that helps a bit.
Last edited by Zaron X; Apr 23, 2014 @ 10:18am
HeadcrabPL Apr 23, 2014 @ 11:07pm 
Thanks both of you guys.
Guerilla Apr 29, 2014 @ 7:53pm 
if distance_to_object(soldier_obj) > distance_to_object(civillian_obj) target = civillian_obj
if distance_to_object(soldier_obj) < distance_to_object(civillian_obj) target = soldier_obj

See, 3 lines of code. Done.
< >
Showing 1-4 of 4 comments
Per page: 15 30 50

Date Posted: Apr 22, 2014 @ 11:27pm
Posts: 4