- Home /
Area of Effect damage trigger
Hello, I am trying to make a script in my game that creates an explosion when an object dies.
I currently have vials in my game, which can receive damage to the point when their health goes below zero, at which point they are removed from the game. As they are removed, however, I want all enemies (and perhaps the player) to take damage (and potentially experience knockback) within a certain radius. I have an explosion script that should be activated when the item "dies," yet it doesn't activate. I need help identifying this problem.
Also, I plan on havinga blue smokey particle effect to appear when the vial is broken, and this is feature currently not added. Is it necessary to attach the explosion script to the particles, or can it stay on the vial for now?
Here's the explosion script:
using UnityEngine;
using System.Collections;
public class LinearExplosion : MonoBehaviour
{
public float damageRadius = 50;
public float damage = 500;
public int force = 160;
public bool affectPlayer = false; //Apply damage to Player?
public bool addExplosionForceToPlayer = false; //only works when Player is rigidbody
public bool addExplosionForceToEnemies = true; //only works when Enemies are rigidbodies
RaycastHit hit;
public void Explode()
{
print ("success");
//*** Applying damage to Player ***\\
GameObject Player = GameObject.FindGameObjectWithTag("Player");
PlayerHealth playerHealth = Player.GetComponent<PlayerHealth>();
if(Physics.Linecast(this.transform.position, Player.transform.position, out hit))
{
if(hit.collider.gameObject.CompareTag("Player"))
{
if(Vector3.Distance(Player.transform.position, this.transform.position) < damageRadius)
{
float proximity = (this.transform.position - Player.transform.position).magnitude;
float effect = 1 - (proximity / damageRadius);
hit.collider.SendMessageUpwards("ApplyDamage", damage * effect, SendMessageOptions.DontRequireReceiver);
if(hit.collider.rigidbody && addExplosionForceToPlayer)
{
hit.collider.rigidbody.AddExplosionForce(force, this.transform.position, damageRadius, 3.0f);
}
}
}
}
//*** Applying damage to enemies ***\\
GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Shootable"); //FINDS ENEMIES BASED ON TAG
foreach (GameObject Enemy in Enemies)
{
EnemyHealth enemyHealth = Enemy.GetComponent<EnemyHealth>();
if(!enemyHealth)
{
continue;
}
if(Physics.Linecast(this.transform.position, Enemy.transform.position, out hit))
{
if(hit.collider.gameObject.CompareTag("Shootable"))
{
if(Vector3.Distance(Enemy.transform.position, this.transform.position) < damageRadius)
{
float proximity = (this.transform.position - Enemy.transform.position).magnitude;
float effect = 1 - (proximity / damageRadius);
hit.collider.SendMessageUpwards("ApplyDamage", damage * effect, SendMessageOptions.DontRequireReceiver);
if(hit.collider.rigidbody && addExplosionForceToEnemies)
{
hit.collider.rigidbody.AddExplosionForce(force, this.transform.position, damageRadius, 3.0f);
}
}
}
}
}
//*** Applying force to rigidbodies ***\\
Collider[] colliders = Physics.OverlapSphere(this.transform.position, damageRadius);
foreach(Collider colliderHit in colliders)
{
if(!colliderHit)
{
continue;
}
if(Physics.Linecast(this.transform.position, colliderHit.transform.position, out hit))
{
if(hit.rigidbody)
{
hit.rigidbody.AddExplosionForce(force, this.transform.position, damageRadius, 3.0f);
}
}
}
}
}
And here is the enemy health script, which should activate the explosion script when the enemy instance is killed.
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public Rigidbody food;
public Rigidbody food2;
public Rigidbody food3;
public Rigidbody food4;
public Rigidbody food5;
//public Rigidbody bleeditem;
CapsuleCollider capsuleCollider;
bool isDead;
void Awake ()
{
capsuleCollider = GetComponent <CapsuleCollider> ();
currentHealth = startingHealth;
}
void Update ()
{
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
if(isDead)
return;
currentHealth -= amount;
//Instantiate (food2, transform.position, transform.rotation);
if(currentHealth <= 0)
{
Death ();
if (explodetoggle == 1)
{
new LinearExplosion ();
}
}
}
void Death ()
{
new LinearExplosion();
isDead = true;
capsuleCollider.isTrigger = true;
Destroy (gameObject);
ItemSpawn ();
}
void ItemSpawn ()
{
Instantiate (food, transform.position, transform.rotation);
Instantiate (food2, transform.position, transform.rotation);
Instantiate (food3, transform.position, transform.rotation);
Instantiate (food4, transform.position, transform.rotation);
Instantiate (food5, transform.position, transform.rotation);
}
public void StartSinking ()
{
GetComponent <NavMeshAgent> ().enabled = false;
GetComponent <Rigidbody> ().isKinematic = true;
}
}
Answer by Berenger · Jan 19, 2015 at 08:27 AM
You can't instantiate monobehaviours like other objects, you have to use the functions Unity provides. You can either :
Create a new GameObject (GameObject go = new GameObject()), then use the AddComponent().
Or declare a public LinearExplosion in your enemy script, assign a prefab to it in the editor, then use GameObject.Instantiate.
=> You'll have to call Explode manually.
Answer by AndresBarrera · Jan 19, 2015 at 08:32 AM
Ok, two things you may want to check:
Unity advises against creating MonoBehaviours and/or GameObjects with the keyword "new". Instead, you should instantiate a Prefab with your LinearExplosion script attached to it. And consider using an object pool to improve performance (avoiding multiple instantiations if you can reuse old objects)
The new object that you instantiate must not be related to the GameObject that you are destroying. Your LinearExplosion script must not be attaached to the same object with EnemyHealth nor be a children, because it will stop its execution when the main object is destroyed.
Hope it helps
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Explosion Jumpscare Script ? 1 Answer
What is the difference between these two scripts? 1 Answer
How to Destroy items in Match 3. 2 Answers