//############################################################################################## // Create the bullet(s) and send them shooting in the direction of muzzleTransform. Also spawn // effects if available. // The argument can be used to change the damage amount (usually called from subclasses to // modify damage) // return value indicates whether or not the gun actually fired. //############################################################################################## public bool Shoot(float damage) { if (gunTimer.Finished() && !reloading) { if (remainingMagazineAmmoCount == 0 && remainingBoxAmmoCount == 0) { return(BULLET_NOT_FIRED); } gunTimer.Start(); shooting = true; remainingMagazineAmmoCount--; if (remainingMagazineAmmoCount == 0 && remainingBoxAmmoCount > 0) { ReloadGun(); } // This is for shotgun-type weapons. It spawns several bullets in a random cone for (int i = 0; i < currentGunData.shots; ++i) { GameObject bulletInstance = null; if (currentGunData.usePooledBullets) { bulletInstance = PooledGameObjectManager.GetInstanceFromPool(currentGunData.poolIdentifier); } else { bulletInstance = GameObject.Instantiate(currentGunData.bulletPrefab); } BulletComponent bullet = bulletInstance.GetComponent <BulletComponent>(); if (currentGunData.usePooledBullets) { bullet.SetAsPooled(currentGunData.poolIdentifier); } if (bullet == null) { Logger.Error("Bullet Prefab " + currentGunData.bulletPrefab.name + " must have a bullet component"); return(BULLET_NOT_FIRED); } // Apply non-zero spread. This can be for shotgun scatter, // or for inaccurate, normal guns Quaternion spreadOffset = Quaternion.identity; if (currentGunData.spread > 0.0f) { spreadOffset = Quaternion.AngleAxis(Random.value * currentGunData.spread, Vector3.right); Quaternion rot = Quaternion.AngleAxis(Random.value * 360.0f, Vector3.forward); spreadOffset = rot * spreadOffset; } Vector3 bulletVelocity = Vector3.zero; bulletInstance.transform.position = muzzleTransform.position + muzzleTransform.TransformDirection(currentGunData.muzzleOffset); bulletInstance.transform.rotation = muzzleTransform.rotation * spreadOffset; bulletVelocity = bulletInstance.transform.forward * currentGunData.muzzleVelocity; // Add in player velocity if necessary if (player != null) { bulletVelocity += player.GetVelocity(); } // Notify the bullet it's been fired bullet.Fire(damage, currentGunData.damageType, bulletVelocity, gameObject); } // Spawn effects if available, outside the loop, so there's only ever one if (currentGunData.firingEffectsPrefab != null) { GameObject effectsInstance = GameObject.Instantiate(currentGunData.firingEffectsPrefab); effectsInstance.transform.parent = muzzleTransform; effectsInstance.transform.localPosition = currentGunData.firingEffectsOffset; } // If there's a casing particle, emit 1 if (optionalCasingParticle != null) { optionalCasingParticle.Emit(1); } // Play firing sound if it exists, outside the loop, so there's only ever one if (currentGunData.fireSound != null) { SoundManagerComponent.PlaySound(currentGunData.fireSound, muzzleTransform.gameObject); } return(BULLET_FIRED); } else { return(BULLET_NOT_FIRED); } }