i followed tutorial but when but the shell does not give damage to player health. i tried accessing health by buttons but it works but shell damage not working the only code i change apart from tutorial is. particles.duration to particles.main.duration
using UnityEngine;
namespace Complete
{
public class ShellExplosion : MonoBehaviour
{
public LayerMask m_TankMask; // Used to filter what the explosion affects, this should be set to “Players”.
public ParticleSystem m_ExplosionParticles; // Reference to the particles that will play on explosion.
public AudioSource m_ExplosionAudio; // Reference to the audio that will play on explosion.
public float m_MaxDamage = 100f; // The amount of damage done if the explosion is centred on a tank.
public float m_ExplosionForce = 1000f; // The amount of force added to a tank at the centre of the explosion.
public float m_MaxLifeTime = 2f; // The time in seconds before the shell is removed.
public float m_ExplosionRadius = 5f; // The maximum distance away from the explosion tanks can be and are still affected.
private void Start ()
{
// If it isn’t destroyed by then, destroy the shell after it’s lifetime.
Destroy (gameObject, m_MaxLifeTime);
}
private void OnTriggerEnter (Collider other)
{
Collider[ ] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask);
for (int i = 0; i < colliders.Length; i++)
{
Rigidbody targetRigidbody = colliders .GetComponent ();
if (!targetRigidbody)
continue;
targetRigidbody.AddExplosionForce (m_ExplosionForce, transform.position, m_ExplosionRadius);
TankHealth targetHealth = targetRigidbody.GetComponent ();
if (!targetHealth)
continue;
float damage = CalculateDamage (targetRigidbody.position);
targetHealth.TakeDamage (damage);
}
m_ExplosionParticles.transform.parent = null;
m_ExplosionParticles.Play ();
m_ExplosionAudio.Play ();
Destroy (m_ExplosionParticles.gameObject, m_ExplosionParticles.main.duration);
}
private float CalculateDamage (Vector3 targetPosition)
{
Vector3 explosionToTarget = targetPosition - transform.position;
float explosionDistance = explosionToTarget.magnitude;
float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius;
float damage = relativeDistance * m_MaxDamage;
damage = Mathf.Max (0f, damage);
return damage;
}
}
}
using UnityEngine;
using UnityEngine.UI;
public class TankHealth : MonoBehaviour
{
public float m_StartingHealth = 100f;
public Slider m_Slider;
public Image m_FillImage;
public Color m_FullHealthColor = Color.green;
public Color m_ZeroHealthColor = Color.red;
public GameObject m_ExplosionPrefab;
private AudioSource m_ExplosionAudio;
private ParticleSystem m_ExplosionParticles;
private float m_CurrentHealth;
private bool m_Dead;
private void Awake()
{
m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent();
m_ExplosionAudio = m_ExplosionParticles.GetComponent();
m_ExplosionParticles.gameObject.SetActive(false);
}
private void OnEnable()
{
m_CurrentHealth = m_StartingHealth;
m_Dead = false;
SetHealthUI();
}
public void TakeDamage(float amount)
{
// Adjust the tank’s current health, update the UI based on the new health and check whether or not the tank is dead.
m_CurrentHealth -= amount;
SetHealthUI ();
if (m_CurrentHealth <= 0f && !m_Dead) {
OnDeath ();
}
}
private void SetHealthUI()
{
// Adjust the value and colour of the slider.
m_Slider.value = m_CurrentHealth;
m_FillImage.color = Color.Lerp (m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);
}
private void OnDeath()
{
// Play the effects for the death of the tank and deactivate it.
m_Dead = true;
m_ExplosionParticles.transform.position = transform.position;
m_ExplosionParticles.gameObject.SetActive (true);
m_ExplosionParticles.Play ();
m_ExplosionAudio.Play ();
gameObject.SetActive (false);
}
}
