The main problem is that if there are multiple turrent AIs in game only one of them will shoot the projecile at the player and other ones will not and I am wondering how to fix it.
public GameObject projectile;
private bool inRange = false;
private Vector3 projectileSpawn;
private Quaternion rotation;
public float fireRate = 1F;
private float nextShot = 0.0F;
public static Transform target;
// Update is called once per frame
private void Update()
{
projectileSpawn = this.transform.parent.GetChild(0).position;
rotation = this.transform.parent.GetChild(0).rotation;
ShootProjectile();
}
private void OnTriggerStay(Collider collision)
{
if (collision.tag == "Human")
{
targeter = GameObject.Find("Human-1").transform;
GameObject.Find("ProjectileSpawn").GetComponent().SetInRange(true);
//SetInRange(true);
Debug.Log("In range of enemy turret");
}
else if (collision.tag == "Human2")
{
targeter = GameObject.Find("Human-2").transform;
GameObject.Find("ProjectileSpawn").GetComponent().SetInRange(true);
//SetInRange(true);
Debug.Log("In range of enemy turret");
}
}
private void OnTriggerExit(Collider collision)
{
if (collision.tag == "Human2" || collision.tag == "Human")
{
GameObject.Find("ProjectileSpawn").GetComponent().SetInRange(false);
// SetInRange(false);
target = null;
Debug.Log("Out of range of enemy turret");
}
} // Resets the value of TurretAI.inRange to false when the astral goes out of range (leaves box collider)
private void ShootProjectile()
{
if (inRange && Time.time > nextShot) // Provided the target is close enough and a set time has past since the last shot, shoot at target
{
nextShot = Time.time + fireRate; // Causes a time delay between each projectile being fired
Instantiate(projectile, projectileSpawn, rotation);
Debug.Log("Shots Fired!");
}
}
private void SetInRange(bool incomingValue)
{
inRange = incomingValue;
}
}
↧