• 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 joms · Apr 01, 2012 at 12:05 AM · c#blenderragdollcubes

Replace ragdoll with smaller cubes

Hello!

I'm creating this ragdoll wich falls to the floor. When it's on the floor and been here for a given time I want to make it look like it's split up in a lot of cubes that flies up and disappears.

The model is made of cubes only, and is placed in a hierarchy so the ragdoll works perfectly. I have also made to iterate through the model and take the Transform properties of each part.

The model looks kind of like this, except the arms is the wrong size after converting from FBX (well done not saving the Blender-model).

Model

After this I don't really know where to go. I would guess that going through each child and find it's real size in Unity, then deciding how many and how large cubes there should be put in would be the thing. But I don't really have a clue how to do this.

I have also thought of replacing the model with another model, but I certainly don't want to do this as I have to remodel both the "original" and the "cuby" one.

C# examples would be preferred.

Does anyone have a hint about how this could be done easily?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Apr 01, 2012 at 03:57 PM

Picasso would love this cubic ragdoll!
If you've created a conventional ragdoll with rigidbodies and character joints, I think the better approach would be to create a "2nd ragdoll" in the Editor: an empty object with childed cubes matching the ragdoll parts in size and name, and replace the ragdoll with this object, copying the transform position and rotation of each part. You should not copy the hierarchy, just add each cube as a child of the empty object - kind of:

  Ragdoll2 <- the root empty object
    Head  <- use the same name of the corresponding ragdoll part
    Chest
    Pelvis
    LArm
    LForearm
    RArm
    RForearm
    LThigh
    LLeg
    RThigh
    RLeg
You could replace the 1st ragdoll with a function like this (1st ragdoll script):

public GameObject rag2prefab; // drag the cubic ragdoll here

void ReplaceMe(){ Destroy(gameObject); // suicide this ragdoll and create the 2nd one: GameObject rag2 = Instantiate(rag2prefab, transform.position, transform.rotation) as GameObject; Rigidbody[] parts = GetComponentsInChildren< Rigidbody>(); // get the ragdoll parts foreach (Rigidbody part in parts){ // iterate through them Transform part2 = rag2.transform.Find(part.name); if (part2){ // if a part with same name exists... part2.position = part.transform.position; // copy position... part2.rotation = part.transform.rotation; // and rotation... // and add the up movement with a little random rotation: part2.rigidbody.velocity = Vector3.up*0.5f; part2.rigidbody.angularVelocity = Random.insideUnitSphere*0.1f; } } } Call the function ReplaceMe() to replace this ragdoll with the Picasso cubic version.
NOTE: Destroy() doesn't kills the object immediately, thus you can safely find its children and copy their positions and rotations while inside the routine ReplaceMe().

EDITED: You could create the cubes on the fly with a code similar to this one, but you should replace all ragdoll colliders with box ones in the Editor. Supposing all ragdoll colliders are box colliders, I think this code could do the job:

  ...
  foreach (Rigidbody part in parts){ // iterate through them
    // create the cube for each part...
    GameObject part2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
    part2.AddComponent< Rigidbody>(); // add a rigidbody to it...
    BoxCollider col = part.collider as BoxCollider; // get the original collider...
    part2.transform.localScale = col.size; // and get the size from it
    part2.transform.position = part.transform.position; // copy position...
    part2.transform.rotation = part.transform.rotation; // and rotation...
    // and add the up movement with a little random rotation:
    part2.rigidbody.velocity = Vector3.up*0.5f; 
    part2.rigidbody.angularVelocity = Random.insideUnitSphere*0.1f;
  }
I'm not sure however about the collider's center: if it's not (0,0,0), the cube may appear at the wrong position. Maybe you should add or subtract the collider's center to the cube position to fix this.
Comment
Add comment · Show 5 · 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 joms · Apr 01, 2012 at 04:04 PM 0
Share

This was the answer I not was hoping for, doing stuff programmatically is a lot more fun :) But I'll give it a shot, in the end it might be the best result as long as I can loop through each of the cubes in the model and modify their position.

avatar image aldonaletto · Apr 01, 2012 at 04:33 PM 0
Share

I think this alternative is better because you can set different colors or materials to each cube, but creating the cubes at runtime is also possible: take a look at my answer, to which I added a code that can do that (maybe with some little changes).

avatar image joms · Apr 01, 2012 at 06:23 PM 0
Share

I'll do a try on the EDIT-part. How would I check if collider's center is at (0,0,0)? And it isn't, how would I set it there? This is the first time I'm creating anything in both Unity3D and Blender. Could you give me an example where one sets e.g. 10 smaller cubes?

Also, what do you mean by "but you should replace all ragdoll colliders with box ones in the Editor."?

At last, I get error at BoxCollider col = part.collider; "Cannot implicitly convert type UnityEngine.Collider' to UnityEngine.BoxCollider'. An explicit conversion exists (are you missing a cast?)"

I've made a GameObject of the ragdoll, and then I do Rigidbody[] robotRigid; void Start() { robotRigid = robotRagdoll.GetComponentsInChildren(); } In Start() I also added your code, and changed from parts to robotRigid.

avatar image aldonaletto · Apr 01, 2012 at 09:41 PM 0
Share

That's why I suggested you to create the 2nd ragdoll in the Editor... Creating cubes based on the current ragdoll may become very complicated! If your ragdoll parts have capsule and/or sphere colliders, you must explore in the Editor the ragdoll hierarchy to find the children that contain the colliders and replace them with box colliders - not an easy task, because the new box collider borns with completely wrong size and center, and you must fix these parameters to adjust it to the part dimensions and position.
About the error: the property collider may contain one of these types: BoxCollider, SphereCollider, CapsuleCollider or $$anonymous$$eshCollider. This assignment works fine in JS, but C# never misses a chance to bother us: we must add "as BoxCollider" to part.collider (what I did now in my answer).
About the centers: the collider position relative to the transform is defined by the center property. To create the cube at the right position, I suspect that you should add (or subtract, I'm not sure) the collider center to the cube position:

   part2.transform.position = part.transform.position+col.center;

avatar image joms · Apr 02, 2012 at 09:21 AM 0
Share

I experimented a bit with it, and decided to make it a model and just replace it. Seemed you were right about the amount of work, and I'm starting to get annoyingly close to the deadline.

avatar image
0

Answer by joms · Apr 02, 2012 at 02:18 PM

I experimented a bit with it, and decided to make it a model and just replace it. Seemed you were right about the amount of work, and I'm starting to get annoyingly close to the deadline.

After I made the cuby model, I find that first of all it's just finding the Root and Spine. This I believe is because it's a quite large hierarchy that goes like this:

  • Root

  • Left_hip

  • Left_leg

  • Right_hip

  • Right_leg

  • Spine

  • Head

  • Left_arm

  • Left_forearm

  • Right_arm

  • Right_forearm

Second: when it finds a part, it just returns this microvalue, e.g. -0.29 in stead of e.g. 230. I really cannot find this giving any sense at all. Any hints?

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 salad__fingers · Aug 12, 2012 at 08:29 PM

Instead of a ragdoll, make all of it a bunch of rigidbodies, that will make it fall on the floor in a bunch of scattered pieces.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Adding collider to 3D model 2 Answers

save my ragdolls current pose in c# 1 Answer

Problem with ragdolls and my armatures 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