Home > Back-end >  How would I make the enemy go towards me?
How would I make the enemy go towards me?

Time:04-02

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.

CodePudding user response:

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);

CodePudding user response:

Unity Basics - Move towards and follow target: video about it

Not really sure if this is what you are looking for. But remember, google is your best friend! ( and the unity documentation even if it can be a little overwhelming some times )

  • Related