- Home /
Explosion Force not working : Unity3d
Hello friends..
Continuing my previous problem.. I want solution for simple problem that how do i add explosion force for my scenario.
According to my scenario , I have created broken parts of zombie like head , heart , hand etc.. and i have attached rigid body and box collider component to all of them.. And all of are child of a parent object..
Now when i shoot my zombie i am replacing that collided object with this broken parts..
I have referred link to add explosion force is this..
But nothing happens..i attached script with this code to all child broken Part object.. What should i do add explosion force effect??
PLEASE help me guys.. Trying to solve this from so many days..
Thanks for your help and support..
Answer by aldonaletto · Oct 07, 2013 at 11:13 AM
Have you ever read the old-and-good-but-no-more-available FPS Tutorial? It has a "dead replacement" code that works with explosions too: the explosion first calculates the damage according to the distance and applies it to each hit object (what gives the victims chance to die), and only then the explosion force is applied. Since the ApplyDamage function replaces the object with its dead replacement when it dies, the explosion force is applied to the ragdoll, not to the character.
Just for convenience, these are the scripts that do the magic:
1) CharacterDamage.js - this script applies damage and, if the character dies, replace it with the dead replacement ragdoll:
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);
}
}
2) Explosion-Advanced.js - this script is attached to the explosion prefab: when the explosion effect is created, the damage is applied in the right order to the neighbor objects:
var explosionRadius = 5.0;
var explosionPower = 10.0;
var explosionDamage = 100.0;
var explosionTimeout = 2.0;
function Start () {
var explosionPosition = transform.position;
// Apply damage to close by objects first
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
// Calculate distance from the explosion position to the closest point on the collider
var closestPoint = hit.ClosestPointOnBounds(explosionPosition);
var distance = Vector3.Distance(closestPoint, explosionPosition);
// The hit points we apply fall decrease with distance from the explosion point
var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
hitPoints *= explosionDamage;
// Tell the rigidbody or any other script attached to the hit object how much damage is to be applied!
hit.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
}
// Apply explosion forces to all rigidbodies
// This needs to be in two steps for ragdolls to work correctly.
// (Enemies are first turned into ragdolls with ApplyDamage then we apply forces to all the spawned body parts)
colliders = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
if (hit.rigidbody)
hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
}
// stop emitting particles
if (particleEmitter) {
particleEmitter.emit = true;
yield WaitForSeconds(0.5);
particleEmitter.emit = false;
}
// destroy the explosion after a while
Destroy (gameObject, explosionTimeout);
}
Have used same code.. still not come up with result/...
Just i want to confirm , is it necessary to enable Use Gravity of rigidbody ?? Because i have disabled use gravity and after enabling its working or its the effect of gravity only.. ??
No, Use Gravity only applies the weight force to the rigidbodies. If it's disabled, the rigidbodies will be launched by the explosion in a straight line, like in outer space.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Exploide GameObjects Into BrokenParts : Unity 1 Answer
Make grenade apply force to rigidbodies around it 3 Answers
Add Explosive Force - Coins 1 Answer
How to make Sphere to jump? 2 Answers