Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed May 01, 2019 at 04:25 PM by edthered1009 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by edthered1009 · Feb 20, 2019 at 10:13 PM · rigidbodyinstantiateexplosion

Instantiated Bomb Not Exploding

I have already figured out how to launch a bomb, but I'm having trouble with running code on the instantiated object. I made it so that the bomb would

  1. Wait

  2. Add an explosive force

  3. Destroy itself

But it only waits, and then disappears.

Here's some code:

  void Update()
     {
         if (time >= timeLimit)
         {
         RB.AddExplosionForce(explosiveForce, bomb.transform.position, explosionRadius);
                 Destroy(bomb);
         }
         time += Time.deltaTime;
       }

Does the explosion only get applied to the rigidbody or something? Send help

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by GuardianWorld · Feb 20, 2019 at 11:57 PM

if you remove the Destroy(gameobject) you placed, the rigidbody that is receiving the explosive force is the bomb. you will see that the object (bomb) was floating after "Exploding".

The AddExplosionForce applies a force that simulates explosion effects to the RigidBody, not to the objects around it.

For that to work, you could:

A) you could create a collider (trigger) around the bomb, and after the time hits it's limit, it will give a explosion force to every rigidbody inside it.

B) You could use a overlap sphere and get all colliders inside and them apply force to them. Code i did way ago:

     private Collider[] hitColliders;
     public float blastRadius;
     public float explosionPower;
     public LayerMask explosionLayers;
 
     void OnCollisionEnter(Collision col)
     {
         //Debug.Log(col.contacts[0].point.ToString());
         ExplosionWork(col.contacts[0].point);
         Destroy(gameObject);
     }
 
     void ExplosionWork(Vector3 explosionPoint)
     {
         hitColliders = Physics.OverlapSphere(explosionPoint, blastRadius, explosionLayers);
 
         foreach (Collider hitCol in hitColliders)
         {
             //Debug.Log(hitCol.gameObject.name);
             if (hitCol.GetComponent<Rigidbody>() != null)
             {
                 hitCol.GetComponent<Rigidbody>().AddExplosionForce(explosionPower, explosionPoint, blastRadius, 1, ForceMode.Impulse);
             }
         }
     }

This code have its flaws, as i did it way before. You could remove the ForceMode.Impulse, or change it (see documentation for this one, it's kinda useful all the modes it have.). This mode i used is a bit more hard to understand, but explanations:

A) It get all the colliders that it hit.

B) Explosion Radius and power;

C) Only hit things in a determined layer (Player, terrain, objects etc.)

It's useful, but first: Your player do have a rigidbody, if the player's rigidbody is Kinematic, then you need to disable it to apply force and after the player hits the ground, you need to enable it again or else it will bug.

If the player is not kinematic (meaning it will only use physics when code dictates to), then the player need to have its X and Z rotation fixed, otherwise he will start spinning like crazy.

Edit: With this code, the bomb will explode after contact with the ground (OnCollisionEnter), you can change that to an update() where it will after some time, explode like normal.

Comment
Add comment · Show 1 · 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 edthered1009 · Feb 21, 2019 at 02:42 AM 0
Share

Oh, I didn't know that. Thanks!

Follow this Question

Answers Answers and Comments

153 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Earth quake with physics? 1 Answer

Rotation problem with spawning bullet 2 Answers

OnCollisionEnter is slow... 2 Answers

I need help with rotation 2 Answers

Rigidbody load problem 1 Answer


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