• Unity
  • Services
  • Made with Unity
  • Learn
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ustulo · Jan 19, 2015 at 08:08 AM · c#triggerexplosionaoe

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;
  
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by AndresBarrera · Jan 19, 2015 at 08:32 AM

Ok, two things you may want to check:

  1. 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)

  2. 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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can requirecomponent specify IsTrigger on a collider? 1 Answer

unityengine.animator does not contain a definition for parameters 0 Answers

Adjusting location on save to avoid infinite loop. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges