Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to move all enemy towards player but i can't do this.Is there any way by which i c an perform this task?

share|improve this question

closed as too broad by bummzack, Sean Middleditch, Anko, Seth Battin, Byte56 Jul 25 '13 at 13:11

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

What did you try? How do you move single units? – Marton Apr 3 '13 at 12:12
i tried to move player but i want movement of player the enemy should follow the player and shoots the player. player is moving perfectly but enemy not? – Maan Apr 3 '13 at 12:15
Search for Steering Behaviors, here a serie of articles about the topic: Understanding Steering Behaviors – JCM Apr 3 '13 at 14:05
up vote 1 down vote accepted

I provide you information about how to code this thing. For example we have to create two classes player and enemy.

Player class contains code about player movement and firing of player towards enemy.

Enemy class contains code about enemy movement towards player and firing of enemy. For enemy movements towards player you have to use MoveModifier and provide data about player in this modifier.

This thing work perfect when player set at constant position but create problem when you move player object. To make this correct you have to unregister MoveModifier that was previously created and attach new MoveModifier with enemy.

Same thing goes repeated with all the enemies. Only once you have to prepare enemy class and your task is complete. I don't provide any code because of no need if you have further problem then add as comment.

share|improve this answer

Get the difference in positions on the x and y axis and use Pythagorean theorem to calculate the length

double dx = player.x - enemy.x;
double dy = player.y - enemy.y;
double length = Math.sqrt(dx * dx + dy * dy);

Normalize the differences in positions

double accelerationX = dx / length;
double accelerationY = dy / length;

Add the acceleration multiplied by a speed determined by you to move the enemy.

enemyPosition.x += (accelerationX * speed);
enemyPosition.y += (accelerationY * speed);

This is the linear algebra way. There is also a trigonometry way, however, this works fine.

Note: I am on my phone so this is all un tested and you will need to modify it to work with your code.

P.s - this code should be placed in an update method that gets called a certain amount of times per second

share|improve this answer

If you are using vectors:

Vector2 direction = player.position - enemy.position;
enemy.position += direction * enemy.speed;
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.