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 Ekta-Mehta-D · Oct 07, 2013 at 10:26 AM · forceexplosion

Explosion Force not working : Unity3d

Hello friends..

Continuing my previous problem.. I want solution for simple problem that how do i add explosion force for my scenario.

According to my scenario , I have created broken parts of zombie like head , heart , hand etc.. and i have attached rigid body and box collider component to all of them.. And all of are child of a parent object..

Now when i shoot my zombie i am replacing that collided object with this broken parts..

I have referred link to add explosion force is this..

But nothing happens..i attached script with this code to all child broken Part object.. What should i do add explosion force effect??

PLEASE help me guys.. Trying to solve this from so many days..

Thanks for your help and support..

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
0
Best Answer

Answer by aldonaletto · Oct 07, 2013 at 11:13 AM

Have you ever read the old-and-good-but-no-more-available FPS Tutorial? It has a "dead replacement" code that works with explosions too: the explosion first calculates the damage according to the distance and applies it to each hit object (what gives the victims chance to die), and only then the explosion force is applied. Since the ApplyDamage function replaces the object with its dead replacement when it dies, the explosion force is applied to the ragdoll, not to the character.

Just for convenience, these are the scripts that do the magic:

1) CharacterDamage.js - this script applies damage and, if the character dies, replace it with the dead replacement ragdoll:

 var hitPoints = 100.0;
 var deadReplacement : Transform;
 var dieSound : AudioClip;
 
 function ApplyDamage (damage : float) {
     // We already have less than 0 hitpoints, maybe we got killed already?
     if (hitPoints <= 0.0)
         return;
 
     hitPoints -= damage;
     if (hitPoints <= 0.0)
     {
         Detonate();
     }
 }
 
 function Detonate () {
     // Destroy ourselves
     Destroy(gameObject);
     
     // Play a dying audio clip
     if (dieSound)
         AudioSource.PlayClipAtPoint(dieSound, transform.position);
 
     // Replace ourselves with the dead body
     if (deadReplacement) {
         var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
         
         // Copy position & rotation from the old hierarchy into the dead replacement
         CopyTransformsRecurse(transform, dead);
     }
 }
 
 static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
     dst.position = src.position;
     dst.rotation = src.rotation;
     
     for (var child : Transform in dst) {
         // Match the transform with the same name
         var curSrc = src.Find(child.name);
         if (curSrc)
             CopyTransformsRecurse(curSrc, child);
     }
 }

2) Explosion-Advanced.js - this script is attached to the explosion prefab: when the explosion effect is created, the damage is applied in the right order to the neighbor objects:

 var explosionRadius = 5.0;
 var explosionPower = 10.0;
 var explosionDamage = 100.0;
 var explosionTimeout = 2.0;
 
 function Start () {
     
     var explosionPosition = transform.position;
 
     // Apply damage to close by objects first
     var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
     for (var hit in colliders) {
         // Calculate distance from the explosion position to the closest point on the collider
         var closestPoint = hit.ClosestPointOnBounds(explosionPosition);
         var distance = Vector3.Distance(closestPoint, explosionPosition);
 
         // The hit points we apply fall decrease with distance from the explosion point
         var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
         hitPoints *= explosionDamage;
 
         // Tell the rigidbody or any other script attached to the hit object how much damage is to be applied!
         hit.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
     }
 
     // Apply explosion forces to all rigidbodies
     // This needs to be in two steps for ragdolls to work correctly.
     // (Enemies are first turned into ragdolls with ApplyDamage then we apply forces to all the spawned body parts)
     colliders = Physics.OverlapSphere (explosionPosition, explosionRadius);
     for (var hit in colliders) {
         if (hit.rigidbody)
             hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
     }
     
     // stop emitting particles
     if (particleEmitter) {
         particleEmitter.emit = true;
         yield WaitForSeconds(0.5);
         particleEmitter.emit = false;
     }
     
     // destroy the explosion after a while
     Destroy (gameObject, explosionTimeout);
 }
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 Ekta-Mehta-D · Oct 07, 2013 at 11:48 AM 0
Share

Have used same code.. still not come up with result/...

avatar image Ekta-Mehta-D · Oct 07, 2013 at 12:12 PM 0
Share

ok. thanks. it is working..

avatar image Ekta-Mehta-D · Oct 07, 2013 at 12:15 PM 0
Share

Just i want to confirm , is it necessary to enable Use Gravity of rigidbody ?? Because i have disabled use gravity and after enabling its working or its the effect of gravity only.. ??

avatar image aldonaletto · Oct 07, 2013 at 01:09 PM 0
Share

No, Use Gravity only applies the weight force to the rigidbodies. If it's disabled, the rigidbodies will be launched by the explosion in a straight line, like in outer space.

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

16 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

Related Questions

A node in a childnode? 1 Answer

Exploide GameObjects Into BrokenParts : Unity 1 Answer

Make grenade apply force to rigidbodies around it 3 Answers

Add Explosive Force - Coins 1 Answer

How to make Sphere to jump? 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