Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Add damage to object when in Physics.OverlapSphere radius.

Discussion in 'Physics' started by ellis1020, Jan 31, 2021.

  1. ellis1020

    ellis1020

    Joined:
    Sep 23, 2017
    Posts:
    19
    So I am using the following code for a grenade script. I've been trying to find a way to make it so when the player or enemies are in the radius it takes health away from them. Does anyone know how I can do this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Grenade : MonoBehaviour
    6. {
    7.     public float movSpeed;
    8.     public float explosionSpeed;
    9.  
    10.     public Enemy enemy;
    11.     public float radius;
    12.     public float power;
    13.  
    14.     public GameObject explosionVFX;
    15.  
    16.     public Vector3 targetPosition;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         this.GetComponent<Rigidbody>().AddForce(transform.forward * movSpeed);
    21.  
    22.         StartCoroutine(BombTimer());
    23.  
    24.         //Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.        
    31.     }
    32.  
    33.     IEnumerator BombTimer()
    34.     {
    35.         yield return new WaitForSeconds(explosionSpeed);
    36.         Explode();
    37.     }
    38.  
    39.     public void Explode ()
    40.     {
    41.    Vector3 explosionPos = transform.position;
    42.         Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
    43.  
    44.         GameObject explosion = Instantiate(explosionVFX, transform.position, Quaternion.identity);
    45.         explosion.transform.parent = this.transform;
    46.  
    47.         foreach (Collider hit in colliders)
    48.         {
    49.             Rigidbody rb = hit.GetComponent<Rigidbody>();
    50.  
    51.            
    52.            
    53.  
    54.             if (rb != null)
    55.                 rb.AddExplosionForce(power, explosionPos, radius, 3.0f);
    56.         }
    57.         Destroy(this.gameObject);
    58.     }
    59. }
    60.  
     
  2. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    You can add this in the foreach loop:
    Code (CSharp):
    1. var player = col.GetComponent<YourCompoenetName>();
    2. if(player != null)
    3. {
    4.     player.AddDamageFunction(damage);
    5. }
     
    InsaneDuane likes this.
  3. InsaneDuane

    InsaneDuane

    Joined:
    Nov 1, 2019
    Posts:
    9
    These two lines together means the instantiated explosion will be destroyed when the gameObject is destroyed. I advise removing the "explosion.transform.parent = this.transform;" command line.