#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Enemy AI - Move away from many instances of player bullets

Posts: 47Member, BASIC

I'm working on a game where the enemy is constantly gliding away from player bullets. The Enemy XPosition is fixed, Y is the variable.
So basically what I'm trying to achieve is that the Enemy is constantly keeping track of where the Player is firing bullets and steers clear of them, until overwhelmed by the Player's aim.
Any help with this would be great!

• Posts: 11,408Member, Sous Chef, PRO, Senior Sous-Chef

I would think you could calculate the destination point of a bullet and then have the enemy move up if the bullet is below it and down if the bullet is above it. Then just re-calculate when each new bullet is fired. It's simplistic but could be a starting point for getting this working.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Posts: 47Member, BASIC

That could work. But if we consider the player's ability to fire multiple bullets, AI might lose its mind and not figure out where to go. I suppose I'm dealing with 'AI ability'.

• Posts: 9Member, BASIC

Is the player shooting along the x axis? Sounds like you'd have to do some math, like what if each time it shoots you 'record' a y-value, like 32, 64, -23, 45, 1, 112, -535, etc. then every few seconds or so you find the average and tell the enemies to "run away from the average": by knowing what the average is you can use 'magnitude' to compare the distance between 'the average y-value and the enemy' and if the distance is less than '32' maybe they move slightly faster.

• London, UKPosts: 2,283Member

If there is only one player, they can only shoot one bullet at one time? There can be more bullets on screen, but if they have only one gun, it will be a sequence of bullets, never two bullets at exactly the same time.

What you also know, based on the distance and the speed of the bullet, the time when a bullet would reach the position where the enemy is (you can calculate that). So then the AI just needs to know not to be in that position at that time, he doesn't need to 'watch' all the bullet all the time.

• Posts: 47Member, BASIC

@a64studios said:
Is the player shooting along the x axis? Sounds like you'd have to do some math, like what if each time it shoots you 'record' a y-value, like 32, 64, -23, 45, 1, 112, -535, etc. then every few seconds or so you find the average and tell the enemies to "run away from the average": by knowing what the average is you can use 'magnitude' to compare the distance between 'the average y-value and the enemy' and if the distance is less than '32' maybe they move slightly faster.

This method could work with trial and error. I like the idea of figuring out the perfect average. But there might be an issue. The player obviously knows where to shoot, right? So he/she will shoot bullets across the x-axis with the Y values you mentioned. Whatever the average of those numbers are, the enemy will move to that spot along the y-axis. At this moment, the player will shoot at that exact spot because, obviously. How does the enemy avoid the bullets now, considering 4 of the 7 bullets shot previously have gone off screen, but now there's a 5th one and more following? Will the timing and distance of the bullets, as well as speed of movement come into play?

• Posts: 47Member, BASIC

@pHghost said:
If there is only one player, they can only shoot one bullet at one time? There can be more bullets on screen, but if they have only one gun, it will be a sequence of bullets, never two bullets at exactly the same time.

What you also know, based on the distance and the speed of the bullet, the time when a bullet would reach the position where the enemy is (you can calculate that). So then the AI just needs to know not to be in that position at that time, he doesn't need to 'watch' all the bullet all the time.

Agree with that. One bullet at a time. However, given that the player is 'better' than the AI, the question is how do I make the AI as smart as or at least half as smart as the player, in order to be able to dodge the bullets continuously and well.

• Posts: 437Member, BASIC
edited October 2017

I would say you need a four game attribute:

Bullet X Position (Real)

Bullet Y Position (Real)

Bullet Count (Index) - this needs to be set to a value of 1.

Bullet ID (Index) - this needs to be set to a value of 1.

When a bullet is spawned, you need to give it an ID. This will be equal to the game.Bullet Count attribute. You then need the bullet to change the game.attribute to +1.
This only occurs once when the bullet is spawned. It means that each bullet will have a unique ID.

In the bullet, you will need a new rule. This is going to check if the bullets self.ID attribute is equal to the game.Bullet ID attribute.

If this is true. It will mean this particular bullet is the the closest to the Enemy than the others. So it will need to** constrain** its X and Y positions to the game attributes.
Now the enemy will know the position of the bullet that is coming towards it.

Then, in the same rule, you will need to check if it has gone passed the enemy, or if it is destroy. If it has (which means its no longer a threat to the enemy), it will need to change the game.Bullet ID attribute to +1.

This will make the second bullet that was spawned follow the exact same process. And so on.

Finally, in your enemy actor, you now know where the closest bullet is at all times.
So you can add in rules accordingly depending on how well you want the enemy to behave.

Simple rules suggested, like if the bullet is above = move down. Bullet is below = move up.

Hope it helps.

Best.

• Posts: 47Member, BASIC

@Two.E said:
I would say you need a four game attribute:

Bullet X Position (Real)

Bullet Y Position (Real)

Bullet Count (Index) - this needs to be set to a value of 1.

Bullet ID (Index) - this needs to be set to a value of 1.

When a bullet is spawned, you need to give it an ID. This will be equal to the game.Bullet Count attribute. You then need the bullet to change the game.attribute to +1.
This only occurs once when the bullet is spawned. It means that each bullet will have a unique ID.

In the bullet, you will need a new rule. This is going to check if the bullets self.ID attribute is equal to the game.Bullet ID attribute.

If this is true. It will mean this particular bullet is the the closest to the Enemy than the others. So it will need to** constrain** its X and Y positions to the game attributes.
Now the enemy will know the position of the bullet that is coming towards it.

Then, in the same rule, you will need to check if it has gone passed the enemy, or if it is destroy. If it has (which means its no longer a threat to the enemy), it will need to change the game.Bullet ID attribute to +1.

This will make the second bullet that was spawned follow the exact same process. And so on.

Finally, in your enemy actor, you now know where the closest bullet is at all times.
So you can add in rules accordingly depending on how well you want the enemy to behave.

Simple rules suggested, like if the bullet is above = move down. Bullet is below = move up.

Hope it helps.

Best.

Going to try this out right away! Thanks! I will update you with what happens. As well as any problems along the way =P