# Multiple Fire Points

Hi, i’m trying to create a 2D game like missile command, i use this script to spawn enemy bullets:

`````` using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyShots : MonoBehaviour
{
public GameObject enemyBullet;
public GameObject enemyFirePoint;
private GameObject[] cities;

public float waitingTime;
public float bulletSpeed;

float timer;

void Start()
{
cities = GameObject.FindGameObjectsWithTag("City");
}
void Update()
{
timer += Time.deltaTime;

if (timer > waitingTime)
{
Vector3 difference = enemyFirePoint.transform.position - cities[Random.Range(0, cities.Length)].transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

float distance = difference.magnitude;
Vector2 direction = difference / distance;

direction.Normalize();
Shoot(direction, rotationZ);

timer = 0;
}
}

void Shoot(Vector2 direction, float rotationZ)
{
GameObject e = Instantiate(enemyBullet) as GameObject;
e.transform.position = enemyFirePoint.transform.position;
e.transform.rotation = Quaternion.Euler(0, 0, rotationZ);
e.GetComponent<Rigidbody2D>().velocity = (direction * -1) * bulletSpeed;
}

}
``````

How can i add multiple fire points (array) to the script? Thanks!

I’ve never played the game, so I’m not exactly sure by your description what you’re looking for.

If your multiple fire points are supposed to be predetermined in the scene before the game starts then you can use a Transform array.

If your points are supposed to be random you can use insideUnitCircle or Random.Range

Here’s a method utilizing a Transform array - You set your transforms up in the scene, drag and drop into the array, and those are your firing points.

``````public Transform[] firePoints = new Transform[3]; // Choose any size you want

void Shoot(Vector2 direction, float rotationZ)
{
Quaternion rotation = Quaternion.Euler(0f, 0f, rotationZ);
foreach (Transform firePoint in firePoints) {
// Instantiate returns GameObject by default, no need to cast
GameObject e = Instantiate(enemyBullet, firePoint, rotation);
e.GetComponent<Rigidbody2D>().velocity = (direction * -1) * bulletSpeed;
}
}
``````

Your firePoints array will also keep rotation and scale, in case you want to spawn bullets with different rotation/scale values.

Hi!

I hope I understood you correctly and the reason for multiple fire points it’s to calculate and call Shoot() function for each one of them. You can do it this way and each time when timer > waitingTime it will iterate through all array and execute your code.

``````public GameObject[] enemyFirePoint;

void Update()
{
timer += Time.deltaTime;

if (timer > waitingTime)
{
for(int i = 0; i < enemyFirePoint.Length; i++)
{
Vector3 difference = enemyFirePoint.transform.position - cities[Random.Range(0, cities.Length)].transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

float distance = difference.magnitude;
Vector2 direction = difference / distance;

direction.Normalize();
Shoot(direction, rotationZ);

timer = 0;
}
}
}
``````

However, keep in mind that it will execute for all point “at once” - in single frame. If you want to make some delay, for example wait couple of second before execute calculations for the second points then you can use IEnumerators like in code below:

``````public GameObject[] enemyFirePoint;

void Update()
{
timer += Time.deltaTime;

if (timer > waitingTime)
{
timer = 0;

StartCoroutine(ExecuteFirePoints());
}
}

IEnumerator ExecuteFirePoints()
{
for(int i = 0; i < enemyFirePoint.Length; i++)
{
Vector3 difference = enemyFirePoint.transform.position - cities[Random.Range(0, cities.Length)].transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

float distance = difference.magnitude;
Vector2 direction = difference / distance;

direction.Normalize();
Shoot(direction, rotationZ);

yield return new WaitForSeconds(1f);
}
}
``````

Remember that for IEnumerators you must have using System.Collections library.
Also, since you are using enemyFirePoint.transform.position in your Shoot() function you can either re-design it or pass additional variable to access certain object in array, or you can apply darksider2000 great method, that’s up to you.

Let me know if it helps you or if I misunderstood your question.