AddExplosionForce on ragdolls

Hi All!

I’m trying to make ragdolls fly extra nice when I hit them with a vehicle, using the AddExplosionForce function.

With the help of two members here we came up with two scripts, but neither seems to have any effect on the ragdolls…

Both the scripts are attached to the car itself, wich runs on the scripts from the car tutorial. The ragdolls are created with the Ragdoll wizzard, and for testing purposes, I use a standard Sphere object, with a rigidbody attached.

EDIT
Here’s a test project with the same setup as we use. The script we’re using is the DarknessBlast.JS, attached to the car. You can see that when hitting the cubes, with the spheres selected as “Ragdoll” in the DarknessBlast script, the spheres seem to be impacted by the AddExplosionForce function just fine… When you select the SoldierRagdoll as “ragdoll” the AddExplosionForce doesn’t work at all, not with or without a rigidbody to the Soldierragdoll’s parent gameobject…

First Script, by @ChrisD:

var ragdoll: GameObject; 
var power : float; 
var radius : float; 
var upModifier : float;
var moveSpeed = 5;

function OnCollisionEnter (collision : Collision) {
    print("collision");
    var colGameObj = collision.gameObject;
    if (colGameObj.tag == "Enemy"){
        Debug.Log("Enemy Success");
        var newRagdoll : GameObject = Instantiate(ragdoll, 
                                          colGameObj.transform.position, 
                                          colGameObj.transform.rotation);
        for (var c in collision.contacts){
            newRagdoll.rigidbody.AddExplosionForce(power, 
                                               c.point, radius,
                                               upModifier);
        }
    Destroy(collision.gameObject);
    }
}

This script instantiates fine, as long as I add a rigidbody to the parent Ragdoll object. If I don’t it just keeps on spawning ragdolls.

I have tried cranking up the power to a 100.000, but the AddExplosionForce doesn’t seem to work, not on the ragdolls, and not on the test-sphere…

The 2nd Script, by @ConfinedDarkness:

var ragdoll : Transform;
var power : float;
var radius : float;
var upModifier : float;

private var canContinue : boolean = true;

	function OnCollisionEnter (collision : Collision) {

	for(var contact in collision.contacts) {

		if(canContinue) {

			if(contact.otherCollider.rigidbody) {

				if(contact.otherCollider.gameObject.tag == "Enemy") {

				var newRagdoll : Transform = Instantiate(ragdoll, contact.otherCollider.transform.position, contact.otherCollider.transform.rotation);

				Destroy(contact.otherCollider.gameObject);
				newRagdoll.rigidbody.AddExplosionForce(power, transform.position, radius, upModifier);
				canContinue = false;

				}
			}
		}
	}
canContinue = true;
}

This script does seem to work with the test-spheres, but also has no explosive effect on the ragdolls, with and without a rigidbody attached to the parent ragdoll object.

Anybody any idea on why one or both of the scripts doesn’t work on raggdolls?

Thanks in advance!

You need to add the force to every rigidbody part of the ragdoll.
One way to do it would be something like this. (Most of this comes
from the FPS tutorial.)

C# code

`
public void Die()
{
	// Destroy ourselves
	Destroy(gameObject);
	if (deadBody != null)
	{
		Transform dead = Instantiate(deadBody, transform.position, transform.rotation) as Transform;    
		CopyTransformsRecurse(transform, dead);
		ApplyForce(dead);
	}
}

private void ApplyForce(Transform target)
{
	foreach (Transform t in target)
	{
		Rigidbody r = t.GetComponent();
		if (r != null)
			r.AddExplosionForce(30, transform.position + transform.forward, 10f, 2f);
		ApplyForce(t);
	}
}

public static void CopyTransformsRecurse (Transform src, Transform dst) 
{
	dst.position = src.position;
	dst.rotation = src.rotation;
	
	foreach (Transform child in dst) 
	{
		Transform curSrc = src.Find(child.name);
		if (curSrc)
			CopyTransformsRecurse(curSrc, child);

	}
}
`

I have a ragdoll with force applied and this code works great for me:

gameObject.rigidbody.AddForce(0,180,100, ForceMode.Impulse);

also you may have to declare what part of the ragdoll you apply the force to instead of applying it to the whole ragdoll. example:

root.rigidbody.AddForce(0,180,100, ForceMode.Impulse);//replace gameObject with root, legs, arm or head.

you will have to adjust the amount of force added depending on the mass of the object getting the force applied. Good luck!

Got it! Add a rigid body to SoldierRagdoll, and add a Fixed joint to Pelvis. Make the connected rigidbody of the pelvis be SoldierRagdoll.

(14 minutes ago)ConfinedDarkness !

You need to add the force to every part of the ragdol that has a rigid body to it