/// <summary> /// Executes the full shooting cycle of the weapon. /// Use "yield return StartCoroutine(base.ShootingCycle());" when calling base from subclass. /// </summary> /// <param name="projectileLayer">The collision layer to set the weapon's projectiles to.</param> /// <param name="shotBy">The ship or structure that shot the projectile.</param> /// <param name="baseBody">The optional rigidbody of the base the weapon is attached to.</param> protected virtual IEnumerator ShootingCycle(Layers.Names projectileLayer, GameObject shotBy, Rigidbody baseBody = null) { // Flag _shooting true until final salvo has been shot. _shooting = true; // Loop over all salvos in the shooting cycle. for (int i = 1; i <= SalvosPerShootingCycle; i++) { // Loop over all shots in the salvo. for (int j = 1; j <= ShotsPerSalvo; j++) { // Spawn the current projectile in the salvo and launch it. LaunchProjectile(_projectileSlots[j - 1], projectileLayer, shotBy, baseBody); if (DelayBetweenShots > 0) { yield return(new WaitForSeconds(DelayBetweenShots)); } } // Skip the delay after the final salvo. if (i != SalvosPerShootingCycle) { if (DelayBetweenSalvos > 0) { yield return(new WaitForSeconds(DelayBetweenSalvos)); } } } // All salvos have been shot. _shooting = false; yield break; }
public GameObject Spawn(Vector3 position, Quaternion rotation, Layers.Names projectileLayer, GameObject shotBy, Transform parent = null) { GameObject newProjectile = GameObject.Instantiate(_prefab, position, rotation, parent); Projectile projectileScript = newProjectile.GetComponent <Projectile>(); projectileScript.Stats = this; projectileScript.ShotBy = shotBy; newProjectile.layer = (int)projectileLayer; return(newProjectile); }
/// <summary> /// Spawns a certain projectile based on the ProjectileSlot passed in and launches it. /// Override in subclass to add weapon specific FX, like barrel smoke. /// Don't forget to spawn the projectile or call this base method when overriding in subclass. /// </summary> /// <param name="projectileLayer">The collision layer to set the projectile to.</param> /// <param name="shotBy">The ship or structure that shot the projectile.</param> /// <param name="baseBody">The optional rigidbody of the base the weapon is attached to.</param> protected virtual void LaunchProjectile(ProjectileSlot projectileSlot, Layers.Names projectileLayer, GameObject shotBy, Rigidbody baseBody = null) { // Spawn an instance of the projectile. GameObject newProjectile = projectileSlot.Projectile.Spawn(projectileSlot.SpawnPoint, projectileLayer, shotBy); // Spawn velocity is projectile velocity in forward direction. Vector3 spawnVelocity = projectileSlot.SpawnPoint.transform.forward * ProjectileVelocity; // If a moving base rigidbody was passed in, take a part of its velocity and add it to the spawnVelocity. if (baseBody != null) { spawnVelocity += baseBody.velocity * projectileSlot.Projectile.BaseVelocityMultiplier; } // Add the projectile's starting velocity and its base object's velocity together to launch it. Rigidbody projectileBody = newProjectile.GetComponent <Rigidbody>(); projectileBody.AddForce(spawnVelocity, ForceMode.VelocityChange); PlayRandomShootingSound(); }
/// <summary> /// Starts a coroutine that executes the full shooting cycle of the weapon. /// </summary> /// <param name="projectileLayer">The collision layer to set the weapon's projectiles to.</param> /// <param name="shotBy">The ship or structure that shot the projectile.</param> /// <param name="baseBody">The optional rigidbody of the base the weapon is attached to.</param> public virtual void Shoot(Layers.Names projectileLayer, GameObject shotBy, Rigidbody baseBody = null) { StartCoroutine(ShootingCycle(projectileLayer, shotBy, baseBody)); }