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 04, 2013 at 12:34 PM · rigidbodyvelocityforceexplosion

Exploide GameObjects Into BrokenParts : Unity

Hello Everyone,

I have created brokenparts(head , leg , hand , heart) and all gameobject is having rigidbody , all are child of parent object..

Now on explosion i am adding velocity to rigidbody of that broken parts.

when my projectile collides with healthy gameobject at that time OnCollisionEnter() will be called. Below script i have attached to my projectile..

 #pragma strict
 public var bloodPrefab : GameObject;
 public var brokenPartsPrefab : GameObject;
 
 private var healthbarScript1 : healthBar1;
 private var healthbarScript2 : healthBar2;
 private var cowboyShootScript : cowboyShoot;
 private var curHealth : int;
 private var collisionPosition : Vector3;
 private var objectExplode : boolean = false;
 private var explosionRadius = 5.0;    //The starting radius of the explosion
 private var explosionPower = 10.0;
 
 function Start () {
     cowboyShootScript = GameObject.Find("cowboy").GetComponent("cowboyShoot");
 }
 
 function Update () {
     transform.Translate(cowboyShootScript.hitPoint.x * 2 * Time.deltaTime , cowboyShootScript.hitPoint.y * 2 * Time.deltaTime , 0 , Space.World);
     if(transform.position.x > 12.0f)
     {
         Destroy(gameObject);
     } 
 }
 
 
 function OnCollisionEnter(collision : Collision)
 {
     
     collision.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
     
     if(collision.gameObject.tag == "zombie")
     {
         
         collisionPosition = collision.gameObject.transform.position;
         if(collision.gameObject.GetComponent("healthBar1"))
         {
             healthbarScript1 = collision.gameObject.GetComponent("healthBar1");
             healthbarScript1.curHealth -= 1;
             curHealth = healthbarScript1.curHealth;            
         }
         else if(collision.gameObject.GetComponent("healthBar2"))
         {
             healthbarScript2 = collision.gameObject.GetComponent("healthBar2");
             healthbarScript2.curHealth -= 1;
             curHealth = healthbarScript2.curHealth;            
         }
         
         if(curHealth == 0)
         {
             Destroy(collision.gameObject);
             
             Instantiate(brokenPartsPrefab, collisionPosition , Quaternion.identity);
             var brokenParts = [];
             brokenParts = GameObject.Find("brokenParts(Clone)").GetComponentsInChildren(Rigidbody);
             
             for(var rbChild : Rigidbody in brokenParts)
             {
                   rbChild.velocity = rbChild.rigidbody.velocity/2;
                   rbChild.angularVelocity = rbChild.rigidbody.angularVelocity;
             }
     
             objectExplode = true;
         }
         
         Instantiate( bloodPrefab , collision.contacts[0].point, bloodPrefab.transform.rotation );
         Destroy(gameObject);
         
         objectExplode = false;
     }
 }
 
 function OnCollisionExit(collision : Collision)
 {
     collision.rigidbody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationZ;
 }
 
 function FixedUpdate() {
     if (objectExplode == true) {
  
        // Apply an explosion force to all nearby rigidbodies
        if (explosionRadius<18) {
           explosionRadius+=0.5;
  
           var explosionPos : Vector3 = collisionPosition;
           var colliders : Collider[] = Physics.OverlapSphere (explosionPos, explosionRadius);
  
           for (var hit : Collider in colliders) {
           if (!hit)
           continue;
  
           if (hit.rigidbody) {
               hit.rigidbody.AddExplosionForce(explosionPower, explosionPos, explosionRadius, 3.0);
               }
           }
        }
     } else {explosionRadius=5.0;}
 }

But my brokenparts are not exploiding in the air.. What is wrong in this script?? Or if you guys are having another idea then guide me..

Thanks in advance 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
1

Answer by SilentSin · Oct 04, 2013 at 01:23 PM

1)

 function OnCollisionEnter(collision : Collision)
 {
 collision.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
 
 and
 
 function OnCollisionExit(collision : Collision)
 {
 collision.rigidbody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationZ;
 }


If the thing you hit was a zombie, the projectile will destroy itself and never unfreeze the zombie's constraints.

I can't fathom why you would want to do that. Maybe you have a good reason but I certainly can't see it.

2)

 if(curHealth == 0)
 {
     Destroy(collision.gameObject);

If your health is 0, destroy the object you collided with? Is this what you intended?

3)

 Instantiate(brokenPartsPrefab, collisionPosition , Quaternion.identity);
 var brokenParts = [];
 brokenParts = GameObject.Find("brokenParts(Clone)").GetComponentsInChildren(Rigidbody);

That is the weirdest way of instantiating something I've ever seen. Instantiate() returns the game object it instantiated. So a much better way of doing it would be:

 var brokenParts = Instantiate(brokenPartsPrefab, collisionPosition , Quaternion.identity);
 var bodies = brokenParts.GetComponentsInChildren(Rigidbody);

4) This is your first problem:

 for(var rbChild : Rigidbody in brokenParts)
 {
 rbChild.velocity = rbChild.rigidbody.velocity/2;//This is halving the velocity of each child, which would be 0 anyway because they were just instantiated.
 rbChild.angularVelocity = rbChild.rigidbody.angularVelocity;//This does absolutely nothing. You're setting a number to itself.
 }

I don't know what you actually want to set the velocity to, but I'm guessing the desired result isn't half of 0.

5) This is your second problem.

  objectExplode = true;
 }
  
 //Ignore this line//Instantiate( bloodPrefab , collision.contacts[0].point, bloodPrefab.transform.rotation );
 Destroy(gameObject);
  
 objectExplode = false;

Not only will objectExplode always be false at the end of the function, but you are also destroying the projectile, which means that even if you weren't setting objectExplode to false, FixedUpdate() would never even do its thing anyway.

6)

 if (explosionRadius<18) {
 explosionRadius+=0.5;

You're increasing the radius by 0.5 every frame, meaning it will get from 5 to 18 in 26 frames. It works but its certainly not the way I'd do it. I'd do something more along the lines of explosionRadius += 13 * Time.deltaTime; which would increase it from 5 to 18 over a second, regardless of how fast you have physics set to run.

7)

  for (var hit : Collider in colliders) {
 if (!hit)
 continue;

There is no reason to null check the array you were given by Physics.OverlapSphere. Its not gonna tell you it hit things that don't exist.

Comment
Add comment · Show 1 · 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 05, 2013 at 05:12 AM 0
Share

Thanks sir .. you spent time for my problem..

1st answer : its unfreezing my constraints.

2nd answer : yes i am destroying collided game object..

I have changed my code as you have corrected..

still i have not reach to my solution.. You corrected my code and make me understand whatever i have did..but do u have any other solution??

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

Rigidbody is Pushing Another Rigidbody : Unity 1 Answer

Rigidbody velocity has got different direction than VelocityChange force vector 1 Answer

How can you apply force or set the velocity of a rigidbidy on local axis? 1 Answer

AddForce doesn't work when used with velocity 3 Answers

Rigidbody Character 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