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?
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.
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.
Get the difference in positions on the x and y axis and use Pythagorean theorem to calculate the length
Normalize the differences in positions
Add the acceleration multiplied by a speed determined by you to move the enemy.
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
If you are using vectors: