private void OnValidate() { // Check if a weapon category was specified. if (Category == null) { for (int i = 0; i < _projectileSlots.Count; i++) { _projectileSlots[i] = new ProjectileSlot(null, _projectileSlots[i].SpawnPoint); } return; } else { // Category specified, now make sure all projectiles are supported by the weapon. for (int i = 0; i < _projectileSlots.Count; i++) { if (_projectileSlots[i].Projectile != null) { if (!Category.CanShootProjectile(_projectileSlots[i].Projectile.Category)) { Debug.LogWarning(_projectileSlots[i].Projectile.Category.name + " projectiles are not supported by this weapon's category.", this); // Set projectile to null after logging warning message. _projectileSlots[i] = new ProjectileSlot(null, _projectileSlots[i].SpawnPoint); } } } } }
/// <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(); }