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;
}
}
}
}
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) "
– Danil0v3sBy 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
– Danil0v3sI 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?
– HabitablabaIt 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
– Danil0v3sI'll take a look at it, if you mark the answer as the solution ;)
– Habitablaba