- Home /
How do add force from bullet on death?
Too confusing to explain in the title:
On the final shot to the body when the enemies health = 0, it turns into a ragdoll. I want the bullet prefab to give the ragdoll a push when it hits and the player is dead so that it looks like the player died from the bullet.
So if the gun shoots the enemy point blank in the face, the head will fly backwards bringing the rest of the body with it. How is this done?
Answer by aldonaletto · Sep 09, 2013 at 01:48 AM
This depends a lot on how your character is organized, how you're applying damage to it, how you're replacing it with the ragdoll etc. Based on the old-and-good-but-no-more-available FPS Tutorial, the easiest way to implement this is to apply forces to the object hit with AddExplosionForce: this distributes the forces among the ragdoll rigidbodies according to the distance, thus points nearest to the impact point will be more affected. The script below was adapted from the above mentioned FPS Tutorial (ExplosionAdvanced.js) and produced very reasonable results - attach it to the bullet:
var impactRadius: float = 3.0;
var impactPower: float = 50.0;
var damage: float = 10.0;
function OnCollisionEnter(coll: Collision){
var hit: ContactPoint = coll.contacts[0]; // get contact info
// apply damage to the object hit
coll.transform.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
// if killed, the enemy now has been replaced by a ragdoll - get its colliders in an array:
var colliders: Collider[] = Physics.OverlapSphere(hit.point, impactRadius);
// check if there are rigidbodies among them...
for (var col: Collider in colliders){
var rigid: Rigidbody = col.rigidbody;
if (rigid && !rigid.isKinematic){ // if it's a rigidbody...
// apply a force inversely proportional to the distance from the hit point:
rigid.AddExplosionForce(impactPower, hit.point, impactRadius);
}
}
// destroy the bullet
Destroy (gameObject);
}
This script expects that the enemy has a function ApplyDamage, which will hurt it and, when killed, replace it with a ragdoll. If you're basing your scripts in the FPS Tutorial, the enemies probably have the CharacterDamage.js script. If not, that's the CharacterDamage script:
var hitPoints = 100.0;
var deadReplacement : Transform;
var dieSound : AudioClip;
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0)
{
Detonate();
}
}
function Detonate () {
// Destroy ourselves
Destroy(gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// Replace ourselves with the dead body
if (deadReplacement) {
var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) {
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613113820im_/https://answers.unity.com/themes/thub/images/avi.jpg)