- Home /
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;
}
}
}
}
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.
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) "
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?
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;
}
}
}
}
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.
Your answer
Follow this Question
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