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;
			}
		}
	}
}

1 Answer

1

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.

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 instead. UnityEngine.Object:DestroyImmediate(Object) holdBall:OnCollisionEnter(Collision) (at Assets/Script/holdBall.cs:21) "

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

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?

It worked! Love you bro! <3. Now, I'm trying to get the right physics about "ball-to-ball" collision. Already posted a question about that here. Here's the link: http://answers.unity3d.com/questions/805577/raycast-on-ball-game.html Can you help me with that? <3

I'll take a look at it, if you mark the answer as the solution ;)