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 excdu · Nov 18, 2012 at 10:34 PM · childforcechildrenaddexplosionforceexplode

Add Explosion Force to children?

I am making a game similar to Space Invaders, but all of the aliens are made of cubes. I'm trying to make it so that when I shoot an alien, it will explode from all the cubes it is made of, but I can't seem to get it to work.

Here's the code I have:

 public class EnemyExplode : MonoBehaviour {
     
     public float explosionRadius = 5.0f;
     public float explosionPower = 10.0f;
     
     void OnTriggerEnter() {
         foreach (Transform child in transform) {
             child.rigidbody.useGravity = true;
             child.rigidbody.AddExplosionForce(explosionPower,transform.position,explosionRadius,3.0f);
         }
         collider.enabled = false;
     }
 }

When I shoot an enemy the children just fall.

Comment
Add comment · Show 1
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 whydoidoit · Nov 18, 2012 at 11:08 PM 0
Share

Your power isn't very powerful!

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Nov 19, 2012 at 12:01 AM

The script below worked fine: I set Is Kinematic and Use Gravity in all cubes (kinematic rigidbodies ignore useGravity), and made sure the explosion would only occur when a projectile tagged "Bullet" entered the trigger (the cubes themselves generate OnTriggerEnter when they are created). Also the explosionPower should be increased, but remember that the Inspector has the memory of an elephant, and will keep the old value even if the script is changed - modify explosionPower directly in the Inspector. Notice also that when the explosion occurs, I set isKinematic to false before applying the explosion force - the gravity enters automatically because Use Gravity was already set:

 public class EnemyExplode : MonoBehaviour {
 
     public float explosionRadius = 8.0f;
     public float explosionPower = 100.0f;
 
     void OnTriggerEnter(Collider other) {
         if (other.tag == "Bullet"){
             foreach (Transform child in transform) {
                 child.rigidbody.isKinematic = false;
                 child.rigidbody.AddExplosionForce(explosionPower,transform.position,explosionRadius);
             }
         }
     }
 }
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 excdu · Nov 19, 2012 at 12:02 AM

I figured it out. I had to have transform.position+Random.insideUnitSphere*explosionRadius as the explosion position, and increase the explosion force a bit.

For reference, here's the final code:

 public class EnemyExplode : MonoBehaviour {
     
     public float explosionRadius = 5f;
     public float explosionPower = 1000f;
     
     void OnTriggerEnter() {
         foreach (Transform child in transform) {
             child.rigidbody.useGravity = true;
             child.rigidbody.AddExplosionForce(explosionPower,child.transform.position+Random.insideUnitSphere*explosionRadius,explosionRadius,3.0f);
         }
         collider.enabled = false;
     }
 }
 
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

11 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

Related Questions

Detach child and add force 0 Answers

Access Animation in child 1 Answer

Deactivate Specific Children In Partent 1 Answer

Detect collision with child object? 2 Answers

AddExplosionForce only goes up 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