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 Irin · Jun 01, 2015 at 05:17 AM · grenade

Grenade Object Destruction

So, the problem is my grenade object is not being destroyed after a set time and the particle associated with the explosion effect. I have two scripts, one(Grenade.cs) assigned to the player where the grenade is instantiated and translated forward.

The second script(ExploGren.cs) is attached to the grenade prefab, here the particle is instantiated and the damage is dealt to all NPCs in the damage radius. After 5 seconds pass.

But what happens is the grenade is still in the game world and the explosion keeps refreshing and continuously detonating.

 public class Grenade : MonoBehaviour 
 {
     private float force = 20.0f;//players toss
     public GameObject grenade;//its only destroying the rigidbody not the object
     public Transform player;//where to spawn
 
     int GAmount = 3;
 
     public Font Tinker;
 
 
     // Use this for initialization
     void Start () 
     {
 
     }
 
     // Update is called once per frame
     void Update () 
     {
 
         
                 if(Input.GetKeyDown(KeyCode.G))
                 {
                     if(GAmount > 0)
                     {
                         Rigidbody spawnG = (Rigidbody)Instantiate(grenade, player.position, player.rotation);
                         spawnG.AddTorque(5,10,0);
                         spawnG.AddForce(transform.forward * force);
 
                         GAmount--;
 
                         Destroy(spawnG, 0.5f);
                         
                     }
             }
         }
 
 
 }
 

 public class ExploGren : MonoBehaviour 
 {
     public GameObject explosivo;//the explosive particle
     private GameObject insta;// instatntiation of variables for explosion
 
     private float timer = 0.0f;//zero time
     private float timeTD = 5.0f;//time ot detonate
 
     private float aliveTime = 0.05f;
 
     private int exploDamage = 50;//grenade damage
     private float explosiveRadius = 25.0f;//radious of the explosive damage
 
     Collider[] collidersInRange;
     Vector3 grenadeOrigin;
 
     Health damageByGrenadeExplosion;//decalre health 
 
     // Use this for initialization
     void Start () 
     {
 
         grenadeOrigin = transform.position;
 
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         timer +=1 * Time.deltaTime;//start counting
 
             if(timer >= timeTD)//if timer is equal to detonation, detonate
             {
             collidersInRange = Physics.OverlapSphere(grenadeOrigin, explosiveRadius);
                 
             insta = (GameObject)Instantiate(explosivo, transform.position, transform.rotation);//create the explosion at the grenades position
 
                 foreach(Collider col in collidersInRange)
                     {
                         damageByGrenadeExplosion = col.GetComponent<Health>();
                         if(damageByGrenadeExplosion != null)
                             damageByGrenadeExplosion.Damage(exploDamage);
                     }
             }
         Destroy(insta, aliveTime);
 
 
     }
 }
 
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
4
Best Answer

Answer by Saad_Khan · Jun 01, 2015 at 08:30 AM

In the second script im not sure where you are reseting the "timer" variable, although for starters you should put a null check before instantiating or destroying these objects in both scripts..

 if(timer >= timeTD)//if timer is equal to detonation, detonate
         {
 
             if(insta==null)
             insta = (GameObject)Instantiate(explosivo, transform.position, transform.rotation);//create the explosion at the grenades position
 
             if(insta!=null)
             Destroy(insta, aliveTime);
 
 
         }
 

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 Irin · Jun 04, 2015 at 08:21 AM

The particle destruction for the explosion line i recycled from a muzzle flash script. I basically let the particle exist for .05f of a second. I have the time to detonate, and when it does it only lasts for .05f.

Okay i reset as you said, it worked well, it detonate, applys damage, except, with that restart code it still keeps detonating with a second or so resting period in between each burst. I think the situation can be remedied if i destroy the grenade model, the drawback is Unity wont let me destroy game objects.

Got it. The solution was simple and i feel stupid for not seeing it. So in the explogren.cs under Update i added the following lines: void Update () { timer +=1 * Time.deltaTime;//start counting

             if(timer >= timeTD)//if timer is equal to detonation, detonate
             {
             collidersInRange = Physics.OverlapSphere(grenadeOrigin, explosiveRadius);
 
             insta = (GameObject)Instantiate(explosivo, transform.position, transform.rotation);//create the explosion at the grenades position
 
                 foreach(Collider col in collidersInRange)
                     {
                         damageByGrenadeExplosion = col.GetComponent<Health>();
                         if(damageByGrenadeExplosion != null)
                             damageByGrenadeExplosion.Damage(exploDamage);
                     }
             }
 
             Destroy (insta, aliveTime);
             Destroy (gameObject, 5.0f);
         }


Ill edit this code some more and add documentation, ill post the current one here and on my blog: novicegamedesign.blogspot.com

Thanks for the help Saad_Khan

 public class ExploGren : MonoBehaviour 
 {
     public GameObject explosivo;//the explosive particle
     private GameObject insta;// instatntiation of variables for explosion
 
     private float timer = 0.0f;//zero time
     private float timeTD = 5.0f;//time ot detonate
 
     private float aliveTime = 0.9f;
 
     private int exploDamage = 50;//grenade damage
     private float explosiveRadius = 25.0f;//radious of the explosive damage
 
     Collider[] collidersInRange;
     Vector3 grenadeOrigin;
 
     Health damageByGrenadeExplosion;//decalre health 
 
     // Use this for initialization
     void Start () 
     {
 
         grenadeOrigin = transform.position;
 
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         timer +=1 * Time.deltaTime;//start counting
 
             if(timer >= timeTD)//if timer is equal to detonation, detonate
             {
             collidersInRange = Physics.OverlapSphere(grenadeOrigin, explosiveRadius);
 
             insta = (GameObject)Instantiate(explosivo, transform.position, transform.rotation);//create the explosion at the grenades position
 
                 foreach(Collider col in collidersInRange)
                     {
                         damageByGrenadeExplosion = col.GetComponent<Health>();
                         if(damageByGrenadeExplosion != null)
                             damageByGrenadeExplosion.Damage(exploDamage);
                     }
             }
 
             Destroy (insta, aliveTime);
             Destroy (gameObject, 5.0f);
         }
 }
 

Make sure to attach the above to a grenade prefab ^ Its missing an audio source btw.

 public class Grenade : MonoBehaviour 
 {
     private float force = 20.0f;//players toss
     public GameObject grenade;//its only destroying the rigidbody not the object
     public Transform player;//where to spawn
 
     int GAmount = 3;
 
     public Font Tinker;
 
 
     // Use this for initialization
     void Start () 
     {
 
     }
 
     // Update is called once per frame
     void Update () 
     {
 
         
                 if(Input.GetKeyDown(KeyCode.G))
                 {
                     if(GAmount > 0)
                     {
                         Rigidbody spawnG = (Rigidbody)Instantiate(grenade, player.position, player.rotation);
                         spawnG.AddTorque(5,10,0);
                         spawnG.AddForce(transform.forward * force);
 
                         GAmount--;
 
                         
                         
                     }
             }
 
         }
 
     void OnGUI()
     {
         GUIStyle TinkerFont = new GUIStyle();
         TinkerFont.font = Tinker;//this works, changes the hud to Tinker Font.
         TinkerFont.fontSize = 21;
         TinkerFont.normal.textColor = Color.red;
         
         GUI.Label(new Rect(890,520, 75,35),Value(GAmount), TinkerFont);
     }
 
     public string Value(int a)
     {
         string total;
         return total = "GND: " + GAmount;
     }
 }
 
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why doesn't my grenade go anywere 4 Answers

Null reference and GetComponent 1 Answer

Issues NPC Grenade avoidance 3 Answers

Character Controller Grenade problems 1 Answer

How do create a grenade launches? 0 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