I have 30 objects/enemies in my scene and they all fire at exactly the same time.
So I added a random delay into the firing script, hoping to make them fire at different times but they still ‘all’ fire at exactly the same time.
using UnityEngine;
using System.Collections;
public class InvaderShoot : MonoBehaviour
{
//private float delay = Random.Range(1.5f,3f);
private bool canFire = false;
public float attackDistance = 150;
public GameObject bolt;
public Transform boltSpawn01;
public float fireSpeed;
private float fireAgain; //set the time between shots
void Update()
{
RaycastHit hit;
Ray shootingRay = new Ray(transform.position, -Vector3.forward);
//Debug.DrawRay(transform.position, -Vector3.forward * attackDistance);
if(Physics.Raycast(shootingRay, out hit, attackDistance))
{
if(hit.collider.tag == "ShieldCube")
{
canFire = true;
StartCoroutine(InvaderFireWeapon());
}
if(hit.collider.tag == "Enemy")
{
canFire = false;
}
if(hit.collider.tag == "Player")
{
canFire = true;
StartCoroutine(InvaderFireWeapon());
}
}
else
{
canFire = true;
StartCoroutine(InvaderFireWeapon());
}
}
IEnumerator InvaderFireWeapon()
{
float delay = Random.Range(1.5f,3f);
yield return new WaitForSeconds(delay);
print("Waiting For " + delay);
if(canFire == true && Time.time > fireAgain)
{
fireAgain = Time.time + fireSpeed;
Instantiate(bolt, boltSpawn01.position, boltSpawn01.rotation);
}
}
}
I know all 30 are using the same script, but I don’t understand how(or why) they are still all firing at the same time.
Need some help on this one please guys
???