//repeater type public void Repeater(TurretObjects turret) { //when enough time has passed if (Time.time > turret.nextShot) { //the new nextshot is the current time + the time it takes to fire turret.nextShot = Time.time + turret.shotInterval; //fire a bullet GetBullet(turret); } }
//burst type public void Burst(TurretObjects turret) { //when enough time has passed if (Time.time > turret.nextBurst) { //the next burst is the current time+ the time it takes to fire a burst + it's delay turret.nextBurst = Time.time + turret.burstInterval + turret.burstDelay; //a couroutine that fires the burst is started StartCoroutine(BurstShots(turret)); } }
public void ShootCaller(TurretObjects turret) { //fires the specific method depending on turret type switch (turret.turretType) { case TurretObjects.shootingType.None: break; case TurretObjects.shootingType.Burst: Burst(turret); break; case TurretObjects.shootingType.Repeater: Repeater(turret); break; } }
public IEnumerator BurstShots(TurretObjects turret) { int i = 0; //shot interval is determined based on burst interval and the amount of bullets to be fired in said //interval turret.shotInterval = turret.burstInterval / turret.burstSize; //count the bullets while (i < turret.burstSize) { i++; //wait for the calculated shotinterval yield return(new WaitForSeconds(turret.shotInterval)); //then fire a single bullet GetBullet(turret); } }
//called when firing a bullet public void GetBullet(TurretObjects turret) { //get a bullet from the pool, search by tag set in the unity inspector, on the prefab GameObject bullet = ObjectPooler.sharedInstance.GetPooledObject(turret.usedBulletType.ToString()); //just to be sure we aren't firing blanks if (bullet != null) { //set the bullet's start position at the location of the bullet spawn point bullet.transform.position = turret.turretObj.transform.position; //will go in the direction the gatling gun is pointing towards bullet.transform.rotation = turret.turretObj.transform.rotation; //get the bullet's rigidbody2d component, setting it in the controller variable>(); //and set its velocity bullet.SetActive(true); bullet.GetComponent <Rigidbody2D>().velocity = bullet.transform.right * (turret.bulletSpeed + shipSpeed); } }