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 /
avatar image
0
Question by AlBob75 · Nov 14, 2017 at 01:50 AM · addexplosionforce

AddExplosionForce - what am i doing wrong

Can anyone spot what i'm doing wrong here please?

I've been running through the PushyPixels Space Invaders tutorial, and have hit an issue when adding explosion forces to the particles that are generated when you hit an Invader.

When the invader is hit, it loops through the 8 "gib" prefabs (attached to the enemy via the gibs public array in script) and instantiates each one and then applies the explosion force. But the particles aren't affected by the explosion force for some reason.

What's strange is that if i apply the explosion force after the instantiation loop using same params it works as expected (i used the example from the unity docs for this).

Here is the code:

 public class GibOnCollide : MonoBehaviour
 {
 
   public GameObject[] gibs;
   public float explosionForce;
   public float spawnRadius = 1.0f;
 
   void OnTriggerEnter()
   {
   
     foreach(GameObject gib in gibs)
     {
       GameObject gibInstance = Instantiate(gib, transform.position + Random.insideUnitSphere * spawnRadius, transform.rotation) as GameObject;
       Rigidbody rb = gibInstance.GetComponent<Rigidbody>();
 
       // this doesn't work for some reason!
    //   rb.AddExplosionForce(explosionForce, transform.position, spawnRadius);
     }
 
     
     Collider[] hitColliders = Physics.OverlapSphere(transform.position, spawnRadius);
     foreach (Collider hit in hitColliders)
     {
       Rigidbody rb = hit.GetComponent<Rigidbody>();
       if (rb != null)
       {
         // this does work!!
         rb.AddExplosionForce(explosionForce, transform.position, spawnRadius);
       }
     }
 
     Destroy(gameObject);
   }
 }


I've checked the gib prefab and it has the Rigidbody component attached and is not set as Kinematic.

I'm using Unity 2017.2 BTW.

Thanks, Alan

Comment
Add comment · Show 4
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 jchester07 · Nov 14, 2017 at 06:05 AM 0
Share

Have you tried checking if rb is not null?

avatar image AlBob75 · Nov 16, 2017 at 12:37 AM 0
Share

Hi @jchester07,

I added debug message to check rb and it wasn't null.

I've done some more testing and i can get it to work if i call AddExplosionForce via a coroutine and add a small delay:

   IEnumerator delayedExplosion(Rigidbody rb)
   {
     Debug.Log("Coroutine called");
     yield return new WaitForSeconds(0.000001f);
     Debug.Log("Finished waiting adding explision force now!");
     rb.AddExplosionForce(explosionForce, transform.position, spawnRadius);
   }
 
 void OnTriggerEnter()
   {
     foreach (GameObject gib in gibs)
     {
       GameObject gibInstance = Instantiate(gib, transform.position + Random.insideUnitSphere * spawnRadius, transform.rotation) as GameObject;
       Rigidbody rb = gibInstance.GetComponent<Rigidbody>();
 
       // this only works if called via a coroutine
       StartCoroutine(delayedExplosion(rb));
       
       // this doesn't work for some reason!
       //rb.AddExplosionForce(explosionForce, transform.position, spawnRadius);
     }


Out of curiosity I tried putting a delay directly in the instantiation loop but that didn't work, so it's as if the system needs to move forward a frame or 2 before you can use the AddExplosionForce function.

Has anyone else experienced this, is it normal?

Thanks, Alan

avatar image jchester07 AlBob75 · Nov 16, 2017 at 01:59 AM 0
Share

I tried your original script and its working on my end. Can you try testing it on a new project? There must be other factors affecting it beside the script alone.

avatar image AlBob75 · Nov 17, 2017 at 12:55 PM 0
Share

Hi @jchester07,

O$$anonymous$$, i created a new project to try and re-create the issue.

I've simplified it, there is one "Spawner" object. When press the space key it spawns 4 cubes randomly around the Spawner. For each cube it adds explosion force, with the explosion position being set as the the Spawner position.

So, when i apply explosion force immediately after instantiating the cube the cube gets forced upwards only:
alt text
If i change it so that the explosion force is applied shortly after it is instantiated, however, it looks (to me at least) more correct:
alt text
Below is the script i'm using in the new project:

 public class SpawnCubes : $$anonymous$$onoBehaviour
 {
 
   public GameObject[] cubes;
   public float spawnRadius;
   public float explosionForce;
 
   IEnumerator delayedExplosion(Rigidbody rb)
   {
     yield return new WaitForSeconds(0.01f);
     rb.AddExplosionForce(explosionForce, transform.position, spawnRadius);
   }
 
   // Update is called once per frame
   void Update()
   {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
     {
       foreach (GameObject cube in cubes)
       {
         GameObject cubeInstance = Instantiate(cube, transform.position + Random.insideUnitSphere * spawnRadius, transform.rotation);
 
 
         Rigidbody rb = cubeInstance.GetComponent<Rigidbody>();
 
         // the following doesn't appear to correctly add the explosion force (all of the cubes just go straight up)
         // if ins$$anonymous$$d you comment this line out and uncomment the one below it (StartCoroutine(delayedExplosion(rb)); that seems to work fine)
         // it's as if the AddExplosionForce doesn't work if run immediately after creating the instance
         rb.AddExplosionForce(explosionForce, transform.position, spawnRadius);
 
         //StartCoroutine(delayedExplosion(rb));
 
       }
     }
   }
 }


FYI - here are the Spawner properties:
alt text
And the SmallCube properties:
alt text

smallcube-props.png (47.2 kB)
spawner-props.png (27.1 kB)

0 Replies

· Add your reply
  • Sort: 

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

72 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

Related Questions

Rigidbody.AddExplosionForce goes upwards and not outwards 0 Answers

AddExplosionForce at hit.point 2 Answers

AddExplosionForce Shoots Too High 0 Answers

Default Unity Rigidbody.AddExplosionForce script problem 1 Answer

addExplosionForce radius and force have no effect 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