示例#1
0
    void Shoot()
    {
        timer = 0f;                                 // Reset the timer.
        gunAudio.Play();
        gunLight.enabled = true;                    // Enable the light.
        gunParticles.Stop();                        // Stop the particles from playing if they were, then start the particles.
        gunParticles.Play();
        gunLine.enabled = true;                     // Enable the line renderer and set it's first position to be the end of the gun.

        gunLine.SetPosition(0, transform.position); // gun light has 2 point - reset them
        shootRay.origin    = transform.position;    // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
        shootRay.direction = transform.forward;

        if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))                                  // Perform the raycast against gameobjects on the shootable layer and if it hits something...
        {
            ShootableObjectsHealth enemyHealth = shootHit.collider.GetComponent <ShootableObjectsHealth>(); // Try and find an EnemyHealth script on the gameobject hit.

            if (enemyHealth != null)                                                                        // If the EnemyHealth component exist...0
            {
                enemyHealth.TakeDamage(damagePerShot, shootHit.point);                                      // ... the enemy should take damage.
            }

            gunLine.SetPosition(1, shootHit.point);   // Set the second position of the line renderer to the point the raycast hit.
        }
        else  // If the raycast didn't hit anything on the shootable layer...
        {
            gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);  // ... set the second position of the line renderer to the fullest extent of the gun's range.
        }
    }
示例#2
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_ShootableMask);

            // 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 Shootable objects like player and zombies script associated with the rigidbody.
                ShootableObjectsHealth targetHealth = targetRigidbody.GetComponent <ShootableObjectsHealth> ();

                // If there is no shootable objects script attached to the gameobject, go on to the next collider.
                if (!targetHealth)
                {
                    continue;
                }

                // Calculate the amount of damage the target should take based on it's distance from the shell.
                //float damage = CalculateDamage (targetRigidbody.position);

                // Deal this damage to the tank.
                targetHealth.TakeDamage(10, targetHealth.transform.position);
            }

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

            // Play the particle system.
            m_ExplosionParticles.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(gameObject);
        }