Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • 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
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges