Help with 2D Enemy AI (mostly just the x-axis movement and attacking the player)

As stated in the title I’m looking for some help on the subject of x-axis AI movement and attacking/aiming at the player. I’ve searched everywhere on google and youtube but can’t really find anything matching what i’m trying to do. I couldn’t imagine it being to terribly hard to do seeing that there are no real platform elements (ie drops or ladders) that need to be worried about. In fact I want some of the AI to remain stationary. For the moving AI I just planned on using a simple instantiate upon entering a trigger then having them run in the general direction of the player until they get in range to either shoot or melee.

Any help on the matter or a point in the right direction would be greatly appreciated.

Preferably in Javascript because I’m not very fluent in C#

Thanks Alot!! :smile:

Hi,
If you don’t need real AI. Just something that looks good. You could perhaps use a fake AI?

The concept is to blend random behaviour with spot on behaviour.
You weight the random behaviour to a factor like distance to you enemy.

On a certain distance from enemy, the random behaviour is 100%
On very close distance, fully linear attack is 100%

For example, a walk toward the opponent of the AI at half distance results in 50% random walk in whatever direction and 50% direct towards direction.

Yea this is kinda what I’m wanting to go for, I just want them to instantiate off screen when the player hits a trigger then have them start moving towards the player and upon entering their attack range (either they have a gun or a melee object) they stop and begin attacking. So you are spot on, not real “AI” more of a state based behavior system.

I’m can formulate what needs to be done in my head but when it comes to scripting it I’m a bit lost on where to begin.

I recently switched from flash as3 to unity C# so I can’t help you much with the code.

In as3 I recorded the movement as mouse input in arrays. The reason for the recording of key and mouse input, is that you can really tell if there is human motion or not. We are able to distinguish several different types of motions, like seeking, fleeing, luring, ambushing etc, just by looking how someone moves.

Even if the recorded motions only is a random behaviour, when you mix it with a linear attack motion, it looks really convincing.

So basically, you need to create.

  1. a motion recording function. It can basically be the same you use for the main character, just some arrays to store input.
  2. an enemy that you can record on.
  3. one “linear attack” function
  4. a mixing system of the linear attack and the recorded motion based on distance or other property.