'How would I make the enemy go towards me?
So I've made this basic 2D game and want the enemy to go towards me. I'm new to the location part and just took the code from another post for my movement but can't figure out how to make it move towards me.
private void EnemyAttack_Tick(object sender, EventArgs e)
{
Enemy.Location = new Point(Enemy.Location.X +Player.Location.X, Enemy.Location.Y +Player.Location.Y);
}
If anyone could show me or link a post explaining how I could do it that would be great.
Solution 1:[1]
I am going to guess whats going on here. SInce you gave such a tinu snippet of code. I assume this
| 100
|
| enemy(40,50)
|
|
|
y|
|
| me(10,10)
|______________________________________
0,0 x 100
is you battlefield and you want enemy to move a bit towards 'me', - you dont want it all at once since this is a timer tick.
So lets move 10% for each tick
enemy new x coord = en old xcoord - ((en old xcoord - my x coord) / 10)
enemy new y coord = en old ycoord - ((en old ycoord - my y coord) / 10)
in code
var newx = Enemy.Location.X - ((Enemy.Location.X - Player.Location.X)/10);
var newy = Enemy.Location.Y - ((Enemy.Location.Y - Player.Location.Y)/10);
Enemy.Location = new Point(newx, newy);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
