- Home /
C# Grenade Explosion in Radius
Hello! I am developing my FPS grenades and cannot work out (or find) how to do the damage side of things... I have 'HealthController' scripts on all of the damage-able objects and would like to know how to make it so the grenade does more damage the closer the object is the the explosion
Here is some code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Grenade : MonoBehaviour {
public float delay = 3f;
public float radius = 5f;
public float force = 700f;
public bool hasExploded;
public AudioClip boom;
AudioSource audioSource;
public float beforeDestroy = 10f;
public Rigidbody rb;
public bool startDestroyTimer = false;
public bool isGrounded = false;
public GameObject explosionEffect;
public float countdown;
public HealthController HealthController;
// Use this for initialization
void Start ()
{
audioSource = GetComponent<AudioSource>();
countdown = delay;
}
// Update is called once per frame
void Update ()
{
if (startDestroyTimer == true)
{
beforeDestroy -= Time.deltaTime;
if (beforeDestroy <= 0)
{
Destroy();
}
}
countdown -= Time.deltaTime;
if (countdown <= 0f && !hasExploded)
{
Explode();
hasExploded = true;
}
}
void Explode ()
{
if (CompareTag("Smoke Grenade"))
{
if (isGrounded)
{
rb.isKinematic = true;
}
else
{
Explode();
}
}
audioSource.PlayOneShot(boom, 0.8f);
Instantiate(explosionEffect, transform.position, Quaternion.Euler(-90, 0, 0));
Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
foreach (Collider nearbyObject in colliders)
{
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddExplosionForce(force, transform.position, radius);
}
}
startDestroyTimer = true;
}
void StartDestroyTimer()
{
}
void Destroy()
{
Destroy(gameObject);
}
void OnCollisionStay(Collision collisionInfo)
{
isGrounded = true;
}
void OnCollisionExit(Collision collisionInfo)
{
isGrounded = false;
}
}
and for the Health controller:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HealthController : MonoBehaviour { public float Original_Health = 100f;
public Grenade Grenade;
private void Start()
{
}
private void Update()
{
ColourChange();
}
[SerializeField] public float health = 100f;
public void ApplyDamage(float damage)
{
health -= damage;
if (health <= 0f)
{
Destroy(gameObject);
}
}
void ColourChange()
{
if (this.tag == "Target")
{
if (health >= Original_Health*0.75)
{
GetComponent<Renderer>().material.color = Color.white;
}
else if (health >= Original_Health * 0.5)
{
GetComponent<Renderer>().material.color = Color.green;
}
else if (health >= Original_Health * 0.25)
{
GetComponent<Renderer>().material.color = Color.yellow;
}
else if (health < Original_Health * 0.25)
{
GetComponent<Renderer>().material.color = Color.red;
}
}
}
}
Thank You in Advance!!!!!!
Answer by Hellium · Oct 01, 2018 at 09:11 PM
foreach (Collider nearbyObject in colliders)
{
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddExplosionForce(force, transform.position, radius);
}
HealthController healthController = nearbyObject.GetComponent<HealthController>();
if (healthController != null)
{
healthController.ApplyDamage( GetDamages( Vector3.Distance( nearbyObject.transform.position, transform.position ) ) ) ;
}
}
// ...
// Implement the function fitting your needs
private GetDamages( float distanceFromGrenade )
{
return 1 / distanceFromGrenade ;
// OR
// return 1 / (distanceFromGrenade * distanceFromGrenade) ;
// OR
// return radius * Mathf.Exp( - distanceFromGrenade ) ;
// OR
// ...
}
When i put this in, it says "Since 'Grenade.GetDamages(float)' returns void, a return keyword must not be followed by an object expression" when i hover over the return part at the bottom of your code. The only thing i did was add the word 'void' after 'private' and before 'GetDamages' as there was an error message before
$$anonymous$$y bad, I forgot the return type:
private float GetDamages
Your answer
Follow this Question
Related Questions
Zombie Damage Parts Problem 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Grenade Damage in Radius 2 Answers
Health and Damage [C#] 2 Answers