Space Invaders AI Shooter UNITY3D

3

I have a question about the Spaces Invaders AI:

If you see below in the images of the game, the invaders at the time of shooting, shoot the closest to the player in the form of a grid

That is to say: they will always start shooting the first line, but if I kill any of the first line, the one behind is the one who will start firing.

I made a script that solves it, but I'm not sure if it's at all, the best solution:

public void bulletAI()
{
    invaderList = new List<GameObject> GameObject.FindGameObjectsWithTag("Shooter"));

    var lastId = invaderList.FirstOrDefault(o => 
            o.transform.position.x >= player.transform.position.x);

    Instantiate(invaderProjectile, 
                lastId.transform.position, 
                lastId.transform.rotation);

}

Has anyone encountered something like that? Any feedback is welcome!

PS: Sorry to put it in English, I did not realize it was Stack Overflow Spanish haha. Hugs from Argentina!

    
asked by Pablo Palma 20.01.2016 в 03:11
source

1 answer

1

Well, I did not understand the idea very well, but I will risk to give a solution with what I understood.

If it is a 2d game and you use coordinates (x, y), use Vector2.Distance to calculate the distance of each enemy in relation to the player: For example, the enemy would have this line:

float distance = Vector2.Distance( player.transform.position, transform.position );

So you can get the distance between the player and the enemy.

I hope it serves you.

    
answered by 20.10.2016 в 18:57