Advertisement
Guest User

BFG_Projectile

a guest
Apr 6th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BFG_Projectile : MonoBehaviour
  6. {
  7.  
  8.     [SerializeField] public float speed = 20.0f;
  9.     [SerializeField] public float life = 20f;
  10.  
  11.     public float damageRadius = 10f;
  12.  
  13.     public AudioSource explosion;
  14.     private bool collided;
  15.  
  16.     private void Start()
  17.     {
  18.         Invoke("Explode", life);
  19.     }
  20.  
  21.     private void Update()
  22.     {
  23.         transform.position += transform.forward * speed * Time.deltaTime;
  24.     }
  25.  
  26.     void OnTriggerEnter(Collider col)
  27.     {
  28.         EnemyHealth enemyRef = col.collider.GetComponent<EnemyHealth>();
  29.         Explode();
  30.     }
  31.  
  32.     void Explode()
  33.     {
  34.         Debug.Log("Collision Detected");
  35.         Debug.Log("PlayingSound");
  36.         speed = 0;
  37.         explosion.Play();
  38.  
  39.         GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
  40.         foreach (GameObject Enemy in enemies)
  41.         {
  42.             if (damageRadius >= Vector3.Distance(transform.position, Enemy.transform.position))
  43.             {
  44.                 //Enemy.getComponent(HealthScript).takeDamage(100);
  45.                 EnemyHealth enemyRef = collider.GetComponent<EnemyHealth>();
  46.                 enemyRef.Damage = 5;
  47.                 Debug.Log("Max Damage!");
  48.             }
  49.         }
  50.  
  51.         StartCoroutine(waitForExplosion());
  52.        
  53.     }
  54.  
  55.     IEnumerator waitForExplosion()
  56.     {
  57.         Debug.Log("Waiting for sound to play");
  58.         yield return new WaitWhile(()=> explosion.isPlaying);
  59.         Debug.Log("Destroying");
  60.         Destroy(gameObject);
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement