Advertisement
Guest User

Unity Projectile Collision Detection

a guest
Apr 5th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. void OnTriggerEnter(Collider col)
  2.     {
  3.         if (!collided)
  4.         {
  5.             collided = true;
  6.             Explode();
  7.         }
  8.     }
  9.  
  10.     void Explode()
  11.     {
  12.         Debug.Log("Collision Detected");
  13.         Debug.Log("PlayingSound");
  14.         gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
  15.         explosion.Play();
  16.         StartCoroutine(waitForExplosion());
  17.        
  18.     }
  19.  
  20.     IEnumerator waitForExplosion()
  21.     {
  22.         Debug.Log("Waiting for sound to play");
  23.         yield return new WaitWhile(()=> explosion.isPlaying);
  24.         GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
  25.         foreach (GameObject Enemy in enemies)
  26.         {
  27.             if (damageRadius >= Vector3.Distance(transform.position, Enemy.transform.position))
  28.             {
  29.                 //Enemy.getComponent(HealthScript).takeDamage(100);
  30.                 Debug.Log("Max Damage!");
  31.             }
  32.         }
  33.         Debug.Log("Destroying");
  34.         Destroy(gameObject);
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement