private void OnTriggerEnter(Collider other) { //float accuracy = Vector3.Distance(target.transform.position, owner.transform.position) - Vector3.Distance(target.transform.position, transform.position); ownerTankAgent.AddReward(30 - Vector3.Distance(target.transform.position, transform.position)); // 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; } // Add an explosion force. targetRigidbody.AddExplosionForce(m_ExplosionForce, transform.position, m_ExplosionRadius); // 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; } // 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(damage); ownerTankAgent.AddReward(damage); Debug.Log("HIT"); } // Unparent the particles from the shell. m_ExplosionParticles.transform.parent = null; // Play the particle system. m_ExplosionParticles.Play(); // Play the explosion sound effect. 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); }
public void TakeDamage(float amount) { tankAgent.AddReward(-2 * amount); // Reduce current health by the amount of damage done. m_CurrentHealth -= amount; // Change the UI elements appropriately. SetHealthUI(); // If the current health is at or below zero and it has not yet been registered, call OnDeath. if (m_CurrentHealth <= 0f && !m_Dead) { OnDeath(); } }
private void Fire() { Vector3 opponent_position = transform.InverseTransformPoint(tankAgent.opponent.transform.position); tankAgent.AddReward(-Mathf.Abs(opponent_position.x)); // Set the fired flag so only Fire is only called once. m_Fired = true; // Create an instance of the shell and store a reference to it's rigidbody. Rigidbody shellInstance = Instantiate(m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody; ShellExplosion shellExplosion = shellInstance.GetComponent <ShellExplosion>(); shellExplosion.owner = gameObject; shellExplosion.target = tankAgent.opponent; // Set the shell's velocity to the launch force in the fire position's forward direction. shellInstance.velocity = m_CurrentLaunchForce * m_FireTransform.forward; // Change the clip to the firing clip and play it. m_ShootingAudio.clip = m_FireClip; m_ShootingAudio.Play(); // Reset the launch force. This is a precaution in case of missing button events. m_CurrentLaunchForce = m_MinLaunchForce; }