Newer
Older
HardPoint-Project-Abertay-University-Unity3D / Assets / Scripts / Grenade.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grenade : MonoBehaviour, IAmmunitonHandler
{
    private float damage;
    [SerializeField]private float radius;
    bool exploded= false;
    [HideInInspector]
    public GameObject source;

    SoundsManager sM;

    AudioSource audioSource;
    public void ReadyBullet(float damage, GameObject source)
    {
        GameObject t = GameObject.Find("AudioManager");
        if(t) sM = t.GetComponent<SoundsManager>();
        this.damage = damage;
        this.source = source;
        audioSource = source.GetComponent<AudioSource>();
    }

    private void OnCollisionEnter(Collision collision)
    {
        
        GetComponent<ParticleSystem>().Play();
        Rigidbody rb = GetComponent<Rigidbody>();
        rb.velocity = Vector3.zero;
        rb.angularVelocity = Vector3.zero;

        if (!exploded)
        {
            if (sM) sM.PlaySoundEffect("Explosion", audioSource);
            foreach (HealthSystem h in GameObject.FindObjectsOfType<HealthSystem>())
            {
                if (Vector3.Distance(h.transform.position, transform.position) < radius)
                {
                    if (h.gameObject.tag == "Player") h.TakeDamage(50, source);
                    else
                        h.TakeDamage(damage, source);
                }
            }
            exploded = true;
        }
    }
}