• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by electricsauce · Feb 12, 2012 at 01:20 PM · physicsrigidbodyobject

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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); } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Hooking RigidBody.AddForce 0 Answers

Performance Question on Kinematic Rigidbodies 1 Answer

Dragging ONE rigidbody 1 Answer

Same reaction with different number of objects colliding 2 Answers

Box collider on a moving object-shopping cart 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges