Enemy AI

Hi I am trying to add a simple enemy AI to my platform game - the enemy should run towards the player from the left or the right of the screen (depending on it´s position) and pause it´s movement if he collides with him or something else.

However the main question is how can I achieve something like this? I know how to dispatch custom events but the main problem is the basic structure of my enemy AI. In my game there are many objects updating their positions every frame (like layers), so I am searching for a not so performance heavy method :wink:

I couldn´t find any basic AI tutorial on the web so I am asking directly.

P.S.: I am using Perspective to achieve my parallax effect and I am currently adding the enemy objects to the second layer but the player is moving on the first one.

Do they have to be in the same layer to update their positions correctly?

 As already said I do not want something like mentioned in “The nature of code”, just some basic AI mechanics for moving the newly spawned enemy towards the player.

Help is appreciated,

Max

First to your question with the different layers, cause it’s easier to answer :wink:

The objects don’t have to be in the same layer, but the two layers have to move in exactly the same way if you want to use physics. If you move the layers (groups) different no collision will happen. (or a very buggy one)

Second to the AI question. I have two ideas in mind, but don’t know their hit on performance.

  1. You can just check the difference between the players and the enemys positions in an enter frame loop. If the distance is smaller than a specific amount you call the action “slide towards player” depending on the positions.

  2. You can give the enemy relativly large sensor areas. (like a cross)

    |

—o---

    |

If the player collides with one of them the enemy does the action.

Hope this ideas might help you.

Thank you torbenratzlaff,

I am using physics to check for collisions between bullets and enemies and thought about something like a big sensor area as well.

The problem is that I got 2 or 3 different layers… I will try to put all of my gameObjects into a single one and see if method 1 does not hit too much on performance. There is a limited number of enemies “active” at the same time so I can reduce them if needed.

There may be another method to check if the enemy is able to see the player. I did not use it before but RayCasting might be a viable choice… Okay, I will do some tests today :wink:

If anyone has experience with those different methods please leave a reply.

Max

First to your question with the different layers, cause it’s easier to answer :wink:

The objects don’t have to be in the same layer, but the two layers have to move in exactly the same way if you want to use physics. If you move the layers (groups) different no collision will happen. (or a very buggy one)

Second to the AI question. I have two ideas in mind, but don’t know their hit on performance.

  1. You can just check the difference between the players and the enemys positions in an enter frame loop. If the distance is smaller than a specific amount you call the action “slide towards player” depending on the positions.

  2. You can give the enemy relativly large sensor areas. (like a cross)

    |

—o---

    |

If the player collides with one of them the enemy does the action.

Hope this ideas might help you.

Thank you torbenratzlaff,

I am using physics to check for collisions between bullets and enemies and thought about something like a big sensor area as well.

The problem is that I got 2 or 3 different layers… I will try to put all of my gameObjects into a single one and see if method 1 does not hit too much on performance. There is a limited number of enemies “active” at the same time so I can reduce them if needed.

There may be another method to check if the enemy is able to see the player. I did not use it before but RayCasting might be a viable choice… Okay, I will do some tests today :wink:

If anyone has experience with those different methods please leave a reply.

Max