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 BritishGuy · Nov 03, 2014 at 10:07 PM · colliderexplosionradius

ExplosiveForce Strange effects.

Hello i am attempting to make a rather realistic-ish explosion with a blast wave, I got it working but i am having some strange effects such as..

The force of the blast seem to be stronger when there are more rigid-body objects around But if it blows up with just 1 object it has little effect.

It is like its drawing energy from the surrounding objects to output a bigger blastwave.

Any ideas? here is my code. I have been doing tests in a 0 gravity space like environment. Thanks. Here is a web demo, Hopefully it will explain a little better

Webplayer

     var expandRate = 10.0;
     var radius = 0.0;
     var power = 500.0;
     var losePower = 250.0;
     
     function Update () {
         radius += expandRate * Time.deltaTime;
         collider.radius = radius;
         power -= losePower*Time.deltaTime;
     }
     
      function OnTriggerEnter (other : Collider) {
          var explosionPos : Vector3 = transform.position;
          var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
          
          for (var hit : Collider in colliders) {
              if (!hit)
                  continue;
              
              if (hit.rigidbody && other.tag != "Bullet")
                  hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
          }
      }
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 Linus · Nov 03, 2014 at 10:22 PM 0
Share

Line 21 will make one explosion for each object it collides with. Try using break; or simply return; on line 22 after the first explosion happens.

avatar image BritishGuy · Nov 03, 2014 at 10:31 PM 0
Share

This causes the script to have 0 effect.

avatar image Linus · Nov 03, 2014 at 10:43 PM 0
Share

Trying smaller radius could perhaps help. But I think it may be hard to get a consistent force hitting all object in a predictable manner using ExplosionForce. As the new explosions withh affect other close by rigidbodies.

avatar image BritishGuy · Nov 03, 2014 at 11:02 PM 0
Share

Here is a web demo, Hopefully it will explain a little better

Webplayer

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Nov 03, 2014 at 10:25 PM

The problem is that you will deliver one application of force for every OnTriggerEnter(). The more objects, the more times OnTriggerEnter() is called. And your Update() function is also strange. It means the more time has passed since this object is created, the greater the range and the less the force. I'm not sure why you want this.

My suggestion is to get rid of the Update() code, and have the OnTriggerEnter() code either disable the script or destroy the object the script is attached to after it applies the explosive force once.

Comment
Add comment · Show 4 · 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 BritishGuy · Nov 03, 2014 at 10:29 PM 0
Share

The update is the shock wave, the collier gets bigger over time like a wave would, but over distance it would lose its energy having a lower force. For example, If you throw a rock in a lake, the ripples it causes will only travel so far, The ripples will expand over time but the waves would get smaller.

I already tried to have it destroy the script / deactivate it but then it will only apply a force to the first object it hits.

Here is a web demo, Hopefully it will explain a little better

Webplayer

avatar image robertbu · Nov 04, 2014 at 02:14 AM 0
Share

Cool idea. The error is still the same. You are applying force to all the collider in range for every trigger in the frame. $$anonymous$$ore new objects inside the collider, more trigger events. You can solve this in a couple of different ways. First you can put this at the top of the file:

 private var frame = 0;

Then as the first two lines inside your OnTriggerEnter() function you can do:

 if (frame == Time.frameCount) return;
 frame = Time.frameCount;

This will cause the OnTriggerEnter() to only fire once per frame. You'll likely have to up the force you are adding to compensate.

As a second idea, consider getting rid of the Physics.OverlapSphere(). Then you can use AddExplosionForce() for each OnTriggerEnter() call in the frame. The effect will be different since only new colliders will be impacted each frame.

avatar image robertbu · Nov 04, 2014 at 02:46 AM 0
Share

I decided to play a bit to see what I might do to get a shock wave effect. Here is what I came up with. Space bar start the explosion at the position of the game object with this script.

 #pragma strict
 
 var innerRadius = 0.0;
 var outerRadius = 0.5;
 var speed = 5.0; 
 var maxExplosionRadius = 5.0;
 var explosionForce = 100.0; 
 
 private var exploding = false;
 
 function Update () {
 
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
         exploding = true;
     }
     if (!exploding) return;
     
     var colliders = Physics.OverlapSphere(transform.position, outerRadius);
     for (var col : Collider in colliders) {
         if (Vector3.Distance(transform.position, col.transform.position) > innerRadius) {
             col.rigidbody.AddExplosionForce(explosionForce, transform.position, maxExplosionRadius);
         }
     }
     innerRadius += Time.deltaTime * speed;
     outerRadius += Time.deltaTime * speed;
     if (innerRadius > maxExplosionRadius) {
         Destroy(gameObject);
     }
 }
avatar image BritishGuy · Nov 05, 2014 at 12:46 AM 0
Share

Thanks allot for the help, I have been educated :)

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

27 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

Related Questions

How to emit all particles from one spot at once? 1 Answer

Physics.OverlapSphere wrong radius 0 Answers

How can I destroy a object(like house) with a bomb? 0 Answers

How to do explosion damage to enemy with multiple collider? 1 Answer

Adding explosion force knockback on player 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