How do I get enemy's to chase?


(Cody) #1

I don’t mean to be annoying but how do you get enemy’s to chase you?


#2

Errr, do you mean in a game you are developing yourself? Someone elses game?

A bit more info please :slight_smile:


(Cody) #3

I mean in my game my bad guys don’t chase they just stand still!


#4

well, you have to code in the chasing AI yourself.

Your enemies have a coordinate, i presume, an x/y position?
Your player also has a coordinate, as an x/y position.

The simplest chase AI you can make then, is for each step, your enemy does the following.

    if(me.x < player.x) { me.x++; }
    if(me.x > player.x) { me.x--; }
    if(me.y < player.y) { me.y++; }
    if(me.y > player.y) { me.y--; }

This is basically saying, if my x/y is not equal to the players, then move towards the players by 1 unit. I am amking all sorts of assumptions here, but without knowing more about your game that is all i can do to help you out :slight_smile:


(Cody) #5

Cool thanks heaps! Will come back if I need help


(Cody) #6

how does one make another’s score go down.


(Kevin) #7

@Cody17 The questions you’ve got are great you might checking out some of the tutorials that @crait has put together

This is Part 1, and you’ve probably already got this one down:

But later on some topics about scores, moving enemies and stuff like that is discussed.

This one talks about increment and displaying a value by one, that is kind of like a score:

You might also think about searching online in other game development forums as the questions you are having are more about game design and coding in general than anything specific to the Arduboy.


(Cody) #8

Didn’t really help. If you can could you write it down?


(Boti Kis) #9

There is no code that you can paste and it works.
It depends on what and how you do your game.

There is an amazing book called The Nature of Code about basic mechanics/physics which covers this. And since this is no trivial task, it’s more at the end of the book.
I absolutely can recommend it.


(Cody) #10

Ok thanks will check it out


(Gherca Darius) #11

May I ask a dumb question? In the code that you wrote, i suppose we need to change x and y with the coordonates we gave to the player and the enemy…:no_mouth:


(Pharap) #12

No, the x and y are variables, they change throughout the game.

For example you might have

class Entity
{
public:
  uint8_t x;
  uint8_t y;
};

Entity player;
Entity me;

And then @Bergasms’ code would be somewhere else:

And the enemy “me” would chase player.
Note that this is a very simplified version though, if you want the enemy to go faster than 1 pixel per frame, the code would have to change.