Question is off-topic or not relevant
Script attached to An enemy but dosent work right if there are multiple enemies.
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<TurretEnemy>().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<TurretEnemy>().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<TurretAI>().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;
}
}
Answer by Anon127 · Oct 19, 2018 at 11:26 PM
so how could I have multiple enemies shoot without only one shooting, because only one of them will shoot when the player is in range and the others wont shoot but if the player is in range of any enemy only one specific enemy turrent shoots the projectile.
Follow this Question
Related Questions
How to have multiple enemies (currently having to kill them in order) 1 Answer
How to create a semi-intelligent trigger system? 0 Answers
My enemy ai won't stay on the ground help please 0 Answers
How to load level after typing somthing? 1 Answer
My Unity.exe deleted (Unity 2019.3.11),My Unity.exe was deleted (Unity 2019.3.11) 0 Answers