- Home /
swapping out an object on collision
I'm building a concrete block wall out of a simple mesh that I imported from Blender. I can duplicate them and attach them with a rigid joint that will make them break apart if hit hard or just fall over as a whole if pushed gently.
What I want to do is import a fractured copy of this concrete block and replace the whole block if it struck hard enough. I'm trying to do this because I think Unity will have an easier time keeping track of a few hundred rectangular blocks as opposed to a few thousand block pieces.
Thanks.
Answer by aldonaletto · Feb 12, 2012 at 02:51 PM
This is a "dead replacement" case: an object is destroyed and replaced by a "dead version". The FPS Tutorial gives a good lesson about this in two scripts: DamageReceiver.js and CharacterDamage.js (read also the PDF docs FPS_Tutorial 1 to 3). The first script is used in simple things like barrels, crates, furniture, etc. while the second is used in characters or other complex objects composed by several parts.
The script below is a slightly modified version of CharacterDamage. The function "ApplyDamage" is called by the attacking object via GetComponent or SendMessage; if the damage is applied by an explosion, you must first apply the damage, then apply the explosion forces, so the object is replaced by its dead version before being thrown in the air by the explosion. For this to work, the dead replacement object must have the same hierarchy (some missing children don't produce errors). If you want to replace a wall composed by many objects with a single dead replacement object, no problem: just assign the dead replacement. If there are childed parts, match hierarchy and names of the dead parts to well chosen original equivalents.
var hitPoints = 100.0; // object "health" var deadReplacement: Transform; // drag the "dead version" here var dieSound: AudioClip;
function ApplyDamage (damage : float) { if (hitPoints > 0.0){ hitPoints -= damage; if (hitPoints
function Detonate () { Destroy(gameObject); // Destroy object... // Play a dying audio clip (if any) if (dieSound) AudioSource.PlayClipAtPoint(dieSound, transform.position); // Replace object with the dead body if (deadReplacement){ var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation); // Copy position & rotation from the old object into the dead replacement CopyTransformsRecurse(transform, dead); } }
static function CopyTransformsRecurse (src : Transform, dst : Transform) { dst.position = src.position; dst.rotation = src.rotation; dst.gameObject.active = src.gameObject.active; for (var child : Transform in dst) { // Match the transform with the same name var curSrc = src.Find(child.name); if (curSrc) CopyTransformsRecurse(curSrc, child); } }
Answer by electricsauce · Feb 12, 2012 at 11:28 PM
"If you want to replace a wall composed by many objects with a single dead replacement object, no problem: just assign the dead replacement. If there are childed parts, match hierarchy and names of the dead parts to well chosen original equivalents."
Does this mean that I can't have multiple objects replacing one dead object, or that I have to replace the dead object with with a parent object(ex. an invisible box) that has the block fragments as children?
Thanks for your help, and for that tutorial link. I haven't seen that one yet.