Exemplo n.º 1
0
        private void OnTriggerEnter(Collider other)
        {
            // Collect all the colliders in a sphere from the shell's current position to a radius of the explosion radius.
            Collider[] colliders    = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask);
            bool       destroyShell = true;

            // Go through all the colliders...
            for (int i = 0; i < colliders.Length; i++)
            {
                // ... and find their rigidbody.
                Rigidbody targetRigidbody = colliders[i].GetComponent <Rigidbody>();

                // If they don't have a rigidbody, go on to the next collider.
                if (!targetRigidbody)
                {
                    continue;
                }



                // Add an explosion force.
                targetRigidbody.AddExplosionForce(m_ExplosionForce, transform.position, m_ExplosionRadius);

                // Find the TankHealth script associated with the rigidbody.
                TankHealth tankTargetHealth = targetRigidbody.GetComponent <TankHealth>();

                FurnitureHealth furnitureTargetHealth = targetRigidbody.GetComponent <FurnitureHealth>();

                // Calculate the amount of damage the target should take based on it's distance from the shell.
                float damage = CalculateDamage(targetRigidbody.position);
                // If there is no TankHealth script attached to the gameobject, go on to the next collider.
                if (null != tankTargetHealth)
                {
                    TankShooting shooting = targetRigidbody.gameObject.GetComponent <TankShooting>();
                    if (!shooting)
                    {
                        continue;
                    }

                    if (shooting.m_PlayerNumber == playerNumber)
                    {
                        destroyShell = false;
                        continue;
                    }
                    calculateTankHealth(tankTargetHealth, damage);
                }
                else if (null != furnitureTargetHealth)
                {
                    calculateFurniturehealth(furnitureTargetHealth, 10f);
                }
                else
                {
                    continue;
                }
                destroyShell = true;
            }

            if (destroyShell == false)
            {
                return;
            }

            // Unparent the particles from the shell.
            m_ExplosionParticles.transform.parent = null;

            // Play the particle system.
            m_ExplosionParticles.Play();

            // Play the explosion sound effect.
            if (m_ExplosionAudio)
            {
                m_ExplosionAudio.Play();
            }

            // Once the particles have finished, destroy the gameobject they are on.
            ParticleSystem.MainModule mainModule = m_ExplosionParticles.main;
            Destroy(m_ExplosionParticles.gameObject, mainModule.duration);

            // Destroy the shell.
            Destroy(gameObject);
        }
Exemplo n.º 2
0
 private void calculateFurniturehealth(FurnitureHealth furnitureTargetHealth, float damage)
 {
     // Deal this damage to the furniture.
     furnitureTargetHealth.TakeDamage(damage);
 }