- Home /
Add force on Instantiated Ragdoll
Hello.
I want to make a cloned ragdoll fly by an explosion. I have an animated character and when it dies a ragdoll whit same bones position is cloned. So I can't make the ragdoll fly when the explosion kills the character. I got this so far: On char Script.........
function Dead() { var clone : Transform; clone = Instantiate(ragDollClone, transform.position, transform.rotation); CopyTransformPosition(transform, clone); clone.rigidbody.AddForce(100.0 * (clone.rigidbody.position - _rocketPrefab.explosionPos)); Destroy (gameObject); alreadyDead = true; }
static function CopyTransformPosition (src: Transform, dst: Transform) {
dst.position = src.position;
dst.rotation = src.rotation;
dst.gameObject.active = src.gameObject.active;
for (var child: Transform in dst) {
var curSrc = src.Find(child.name);
if (curSrc) CopyTransformsRecurse(curSrc, child);
}
}
On rocket(Explosion) Script........
function OnCollisionEnter (collision : Collision) {
var explosionOrigin : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionOrigin, radius);
for(var hit : Collider in colliders){
if (hit.rigidbody){
hit.rigidbody.AddExplosionForce(power, explosionOrigin, radius, explosiveLift, ForceMode.Impulse);
explosionPos = explosionOrigin;
Destroy(transform.gameObject);
explosionDamage = (1/((hit.transform.position - explosionOrigin).sqrMagnitude))*160;
hit.transform.SendMessage("ApplyDamage", explosionDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
Thanks in advanced.
Answer by Ozzyel-PT · Sep 04, 2013 at 11:03 PM
I've just figured it out. I had to add the root joint to the script, some like:
clone.Find("Reference/Hips").rigidbody.AddForce((clone.Find("Reference/Hips").position - _rocketPrefab.explosionPos) * Mathf.Abs((1/(clone.Find("Reference/Hips").position - _rocketPrefab.explosionPos).sqrMagnitude))*120, ForceMode.Impulse);
It worked fine for me, character flies in opposite direction of explosion depending on its distance.
Hope it helps someone.
Your answer
Follow this Question
Related Questions
On ragdoll joints, can I apply rotation force? 1 Answer
How to implement Mob movement / death ? 1 Answer
adding force to instantiated ragdoll 4 Answers
Bullet add force to ragdoll help 0 Answers
Disabling Kinematic on hit 2 Answers