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 Daniloleemes · Oct 05, 2014 at 02:46 AM · explosionforces

AddExplosionForce not working

Well, I'm trying to add a explosion force when I release the mouse button, but I have two problems: 1- The joint i'm creating is not destroyed properly and 2- The addexplosionforce doesnt work to me :'(. Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class holdBall : MonoBehaviour {
 
     private bool hold = false;
     private bool criou = false;
 
     void OnCollisionEnter(Collision c){
         if (Input.GetMouseButton (0)) {
             if(criou == false){
                 var joint = gameObject.AddComponent<FixedJoint>();
                 joint.connectedBody = c.rigidbody;
                 hold = true;
                 criou = true;
             }
         }
     else{
             if (hold == true) {
                 var fixed_joint = gameObject.GetComponent<FixedJoint> ();
                 Destroy(fixed_joint);
                 hold = false;
                 criou = false;
             }
         }
     }
 }
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 Habitablaba · Oct 05, 2014 at 06:50 AM

I don't see anything about AddExplosionForce in your code, so I won't comment on that.

As far as your joint destruction issue is concerned:
Destroy is not guaranteed to happen immediately. Or even soon. Try setting the joint's connected body to null to give the impression it has been removed. Then, if you really need the joint to go away right now anyway, try using DestroyImmediate.

Comment
Add comment · Show 8 · 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 Daniloleemes · Oct 05, 2014 at 10:52 AM 0
Share

Well, i removed the explosion part because it was not working. Did the "DestroyImmediate" and got this "Destroying components immediately is not permitted during physics trigger/contact, animation event callbacks or OnValidate. You must use Destroy ins$$anonymous$$d. UnityEngine.Object:DestroyImmediate(Object) holdBall:OnCollisionEnter(Collision) (at Assets/Script/holdBall.cs:21) "

avatar image Daniloleemes · Oct 05, 2014 at 10:57 AM 0
Share

By the way, after the Destroy is read, the next contact on the object thatis connected to the joint moves it away. So I think if we add a explosionforce, it will throw the objects away. Can you help me with this code? :p

avatar image Habitablaba · Oct 06, 2014 at 11:02 PM 0
Share

I think you don't need to bother with DestroyImmediate. Setting the connected body to null should give you the behavior you are looking for. Have you tried this? What happens?
What does your explosion code look like currently?

avatar image Daniloleemes · Oct 08, 2014 at 12:42 AM 0
Share

Here's the code, I got the explosion thing, but it only explodes when other ball touches the player. I want to explode exactly when I release the mouse button. :P I'm almost there, I know hehehe

 using UnityEngine;
 using System.Collections;
 
 public class holdBall : $$anonymous$$onoBehaviour {
 
     public float ExpForce = 200;
     public float ExpRadius = 50;
     public float radius = 500;
     private bool hold = false;
     private bool criou = false;
 
 
 
     void OnCollisionEnter(Collision c){
         if (Input.Get$$anonymous$$ouseButton (0)) {
             if(criou == false){
                 var joint = gameObject.AddComponent<FixedJoint>();
                 joint.connectedBody = c.rigidbody;
                 criou = true;
                 hold = true;
             }
         }
     else{        if(hold == true){
                 var fixed_joint = gameObject.GetComponent<FixedJoint> ();
                 Destroy(fixed_joint,0);
                 Vector3 location = transform.position;
                 Collider[] objectsInRange = Physics.OverlapSphere(location, radius);
                 foreach (Collider col in objectsInRange) {
                     Rigidbody player = col.GetComponent<Rigidbody>();
                     if(player != null){
                         Debug.Log ("Boom!");
                         player.AddExplosionForce(ExpForce, location, ExpRadius);
                     }
                 }
                 hold = false;
                 criou = false;
             }
         }
     }
 }


avatar image Habitablaba · Oct 09, 2014 at 02:17 AM 0
Share

Since you've nested your explosion code inside of your collision check, it'll only explode if there is a collision AND the mouse button is released.
It is interesting that you are using OnCollisionEnter here, but then not doing anything with the colliding object, c.
Does this need to be in OnCollisionEnter, or can you move it all to Update?

If you want to keep the creation code where it is, so it'll only happen when there is a collision and the player presses the mouse button, but want the explosion to happen if the object has been created and the button is released, leave the 'if' part in OnCollisionEnter and move the 'else' to Update. Obviously, you'd want to change 'else' to 'if(Input.Get$$anonymous$$ouseButtonUp(0))' in that case.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Limiting player addforce speed excluding when hit by explosion 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Which is AddExplosionForce relations with rigidibody mass and distance ? 0 Answers

FPS Tutorial 1-What do I drop "Explosion" onto in the inspector window for the "Missile" 3 Answers

Spawning an explosion using an IF statment 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