x


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.

more ▼

asked Feb 12 '12 at 01:20 PM

electricsauce gravatar image

electricsauce
446 9 14 22

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

"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.

more ▼

answered Feb 12 '12 at 11:28 PM

electricsauce gravatar image

electricsauce
446 9 14 22

(comments are locked)
10|3000 characters needed characters left

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 
more ▼

answered Feb 12 '12 at 02:51 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5273
x1948
x1864
x1123

asked: Feb 12 '12 at 01:20 PM

Seen: 483 times

Last Updated: Feb 12 '12 at 11:28 PM