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 GC1983 · Jul 03, 2012 at 07:26 PM · addexplosionforce

How To Work RigidBody.AddExplosionForce()

I found the script for AddExplosionForce(), but I cant seem to get it to respond. Does anyone know if I put this script onto the object that is being forced or to an object that is causing the explosion?

And yes I put a rigidbody on the object I want to be forced. =)

Either way, I tried placing on the rigidbody object and got nothing. Then I tried to place another object under the rigidbody to see if it would be the causer of the force, but still nothing.

Any ideas on how this works?

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

· Add your reply
  • Sort: 
avatar image
1

Answer by Shinugami · Apr 20, 2014 at 09:56 AM

Anything within the radius of the Vector3(where you apply the AddExplosionForce) that has a rigidbody, will be affected. You can use this code in a few ways. The most simple way is to attach it to an empty gameObject and instantiate that object with your explosion object or bullet/missile impact. I'll post my code which works ( the copy and paste of the code isn't exactly the same as the original, some of the formatting is a bit messy in the conversion)

 #pragma strict
     
 var radius : float = 1;    // All objects in this radius with a rigidbody component will be affected
 var power = 10.0;    // how much horizontal power should there be?
 var upForce = 2.0;    // how much vertical power should there be?
 
 private var layerMask : LayerMask; //So we can choose which objects will be added to the Collider Array
 private var myColliders : Collider[];    // Collider Array
 
 function Start () {
 layerMask = 1 << 9 | 1 << 10; // Affect only layers 9 and 10    
 myColliders = Physics.OverlapSphere(transform.position, radius, layerMask);  // Add objects to array that are within radius       
     // Check objects in array and apply explosionforce to appropriate objects.
     for(var hit : Collider in myColliders) {
         if(hit.transform.tag == "Enemy" || hit.transform.tag == "Shootable"){    // Which tagged objects should we apply the explosion to?   
             if(hit && hit.rigidbody){ // if we have an object and that object has a rigidbody then apply the explosion to it
                 hit.rigidbody.AddExplosionForce(power, transform.position, radius, upForce);  // All rigidbodies in the radius will be affected regardless of layer or tag.                   
             }
         }
     }
 }
 Destroy(gameObject); // Now that we have applied the explosionforce we can delete/destroy this object.
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Add Explosion Force to children? 2 Answers

AddExplosionForce not affecting RigidBodies 0 Answers

ExplosionForce On Instantiated Object 1 Answer

Problem with AddExplosionForce / Exploding 1 Answer

How do I get an explosion force to ignore my character's rigidbody? 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