GameMaker: Studio

GameMaker: Studio

Visa statistik:
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
< >
Visar 1-4 av 4 kommentarer
Blyxx 23 apr, 2014 @ 6:50 
Define an object that is the parent of both civilians and soldiers, and have the zombies run to the nearest instance of that object.
Sera 23 apr, 2014 @ 10:16 
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.
Senast ändrad av Sera; 23 apr, 2014 @ 10:18
HeadcrabPL 23 apr, 2014 @ 23:07 
Thanks both of you guys.
libertea 29 apr, 2014 @ 19:53 
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
mp_potential_step(target.x,target.y,5,0)

See, 3 lines of code. Done.
< >
Visar 1-4 av 4 kommentarer
Per sida: 1530 50

Datum skrivet: 22 apr, 2014 @ 23:27
Inlägg: 4