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); // 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; } // Find the TankHealth script associated with the rigidbody. TankHealth targetHealth = targetRigidbody.GetComponent <TankHealth>(); int targetID = targetRigidbody.GetComponent <TankShooting>().m_localID; // If there is no TankHealth script attached to the gameobject, go on to the next collider. if (!targetHealth) { continue; } // Create a vector from the shell to the target. //Vector3 explosionToTarget = targetRigidbody.position - transform.position; // Calculate the distance from the shell to the target. //float explosionDistance = explosionToTarget.magnitude; // Calculate the proportion of the maximum distance (the explosionRadius) the target is away. //float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius; // Calculate damage as this proportion of the maximum possible damage. //float damage = relativeDistance * m_MaxDamage; float damage = m_MaxDamage; // Make sure that the minimum damage is always 0. damage = Mathf.Max(0f, damage); if (targetID != m_playerAttacking) { // Deal this damage to the tank. targetHealth.Damage(damage); } } if (!NetworkClient.active)//if we are ALSO client (so hosting), this will be done by the Destroy so Skip { PhysicForces(); } // Destroy the shell on clients. NetworkServer.Destroy(gameObject); }
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); // 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; } // Find the TankHealth script associated with the rigidbody. TankHealth targetHealth = targetRigidbody.GetComponent <TankHealth>(); // If there is no TankHealth script attached to the gameobject, go on to the next collider. if (!targetHealth) { continue; } // Create a vector from the shell to the target. Vector3 explosionToTarget = targetRigidbody.position - transform.position; // Calculate the distance from the shell to the target. float explosionDistance = explosionToTarget.magnitude; // Calculate the proportion of the maximum distance (the explosionRadius) the target is away. float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius; // Calculate damage as this proportion of the maximum possible damage. float damage = relativeDistance * m_MaxDamage; // Make sure that the minimum damage is always 0. damage = Mathf.Max(0f, damage); // Deal this damage to the tank. targetHealth.Damage(damage); } PhysicForces(); var effect = Instantiate(m_ExplosionParticles, transform.position, transform.rotation); ExplodeShell(effect); Destroy(gameObject); }
private void OnTriggerEnter(Collider other) { // Find all the targets around the explosion and damage them. Collider[] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask); for (int i = 0; i < colliders.Length; i++) { Rigidbody targetRigidbody = colliders[i].GetComponent <Rigidbody>(); if (!targetRigidbody) { continue; } targetRigidbody.AddExplosionForce(m_ExplosionForce, transform.position, m_ExplosionRadius); TankHealth targetHealth = targetRigidbody.GetComponent <TankHealth>(); if (!targetHealth) { continue; } Vector3 explosionToTarget = targetRigidbody.position - transform.position; float explosionDistance = explosionToTarget.magnitude; float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius; float damage = relativeDistance * m_MaxDamage; damage = Mathf.Max(0f, damage); targetHealth.Damage(damage); } m_ExplosionParticles.transform.parent = null; m_ExplosionParticles.Play(); m_ExplosionAudio.Play(); Destroy(m_ExplosionParticles.gameObject, m_ExplosionParticles.duration); Destroy(gameObject); }