Jump to content


Photo

Ally goes toward nearest target


  • Please log in to reply
2 replies to this topic

#1 98nooblets

98nooblets

    GMC Member

  • New Member
  • 39 posts

Posted 16 August 2011 - 02:37 AM

I'm making a platformer game where your character is defenseless, and has allies follow him, which attack enemies when clicked. To make the allies (which are individual objects) follow the character, I use the code (in the step event)
mp_potential_step(character.x,character.y,5,0);
And I use the same for the enemies, except a bit differently:
if (distance_to_object(enemy)< 100)

if (instance_exists(enemy))

mp_potential_step (enemy.x,enemy.y,1,0);
(I change the speed depending on the ally's ability. This one resembles a bomb, and so it follows the character quickly, but follows the enemy slowly. The enemy is just an object that paces imbetween two other objects, which send the enemy in the opposite direction on collision. This works well, but the problem is, because of the speed, (I'm guessing) the allies always go toward the character, even if its farther away. What I hope for it to do is go for whichever object is closer. (which enemy, or character) This way, since you have to click the ally to activate its ability, it will hopefully make it easier to aim, attack, and target. Any ideas?
  • 0

#2 smash ball

smash ball

    Volcanic Light

  • GMC Member
  • 1092 posts
  • Version:GM7

Posted 16 August 2011 - 02:39 AM

You have to check:
if distance_to_object(player)<distance_to_object(instance_nearest(x, y,enemy)) then
//move towards player
else
//move towards enemy

Edited by smash ball, 16 August 2011 - 02:40 AM.

  • 0

#3 98nooblets

98nooblets

    GMC Member

  • New Member
  • 39 posts

Posted 16 August 2011 - 02:51 AM

You have to check:

if distance_to_object(player)<distance_to_object(instance_nearest(x, y,enemy)) then
//move towards player
else
//move towards enemy

Thanks, that even simplified everything I was using before, and works way better than the way I was using before. (I figured it'd make things more complicated) Thanks for the help. :P
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users