New to Unity but not to Houdini. I want to create a fracture setup and get the pieces to get their respective collision geo. Naming them “piece0”, “piece0_collision_geo” etc doesn't work. It results in a object list as in the attachment, all collision geo merged to one piece and without any connection to their piece.
What's the correct way to do this?
Thanks!
Collison geo for multiple pieces
3922 9 2- kahuna031
- Member
- 897 posts
- Joined: July 2018
- Offline
- damian
- Member
- 402 posts
- Joined: March 2013
- Offline
You'll have to rename the pieces themselves to have the “_rendered_collision_geo” postfix in their name to in order to have the collision component be on the same GameObject as the part. There is no way currently in Unity to associate a collision geo with a different visible mesh on the same GameObject.
- kahuna031
- Member
- 897 posts
- Joined: July 2018
- Offline
- damian
- Member
- 402 posts
- Joined: March 2013
- Offline
- kahuna031
- Member
- 897 posts
- Joined: July 2018
- Offline
- damian
- Member
- 402 posts
- Joined: March 2013
- Offline
- kahuna031
- Member
- 897 posts
- Joined: July 2018
- Offline
- kahuna031
- Member
- 897 posts
- Joined: July 2018
- Offline
Ready to use script for this:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class importHoudiniRBD : MonoBehaviour { static void attachCollider(Transform renderXform, Transform colliderXform){ GameObject colObj = colliderXform.gameObject; GameObject renderObj = renderXform.gameObject; //print("Attaching collider: " + colObj.name); MeshCollider collider= colObj.AddComponent<MeshCollider>(); //add collider collider.convex = true; colliderXform.SetParent(renderXform); MeshRenderer colRend = colliderXform.GetComponent<MeshRenderer>(); colRend.enabled = false; //check if allready processed if (null==renderObj.GetComponent<Rigidbody>()) { renderObj.AddComponent<Rigidbody>(); } } static int getChildIndexByName(Transform parent, string name, int numChildren) { int i =-1; for (int ii = 0; ii < numChildren; ii++) { string currentName = parent.GetChild(ii).name; //print(nameSplit[0]); if (currentName.Contains(name+'_'))//'_' and a suffix int is added to the Houdini name { return ii; //found the correct i for current piece } } return i; } public float test = 0; // Adds a sphere collider to all children that doesn't have a collider void Start() { //print("init" + this.gameObject.name); //print("Components: " + this.gameObject.GetComponents(typeof(GameObject))); Transform houObjNode = this.gameObject.transform; int nChildren = houObjNode.childCount; //keep track of number of children as this number gets obsolete as we parent objects print (nChildren); int i = 0; int j = 0; int k = 0; //List<string> pieces = new List<string>(); List<string> colliders = new List<string>(); //get all pieces to attach foreach (Transform childXform in houObjNode) { //if (j > houObjNode.childCount) { continue; } //safety break if (childXform.name.Contains("Collider")) { colliders.Add(childXform.name); } //else { pieces.Add(childXform.name); } } foreach (string colliderName in colliders) { k++; if (k > 1000) { break;} //safety break i = -1; j = -1; string[] nameSplit = colliderName.Split(new string[] { "Collider" }, System.StringSplitOptions.None); string renderObjName = nameSplit[0]; nameSplit = colliderName.Split(new string[] { "Collider" }, System.StringSplitOptions.None); //get transform indices i = getChildIndexByName(houObjNode, renderObjName, nChildren); //check if we found render piece if (i < 0) { Debug.LogError("Couldn't find render mesh: " + renderObjName); //print("i:" + i +", j:" + j); break; } j = getChildIndexByName(houObjNode, colliderName.Split('_')[0], nChildren); if (j < 0) { Debug.LogError("Can't process " + colliderName); } else { Transform renderXform = houObjNode.GetChild(i); Transform colXform = houObjNode.GetChild(j); attachCollider(renderXform, colXform); //print("Render object: " + renderXform); nChildren--; //reduce loop size } } } }
Edited by kahuna031 - March 8, 2017 14:40:30
B.Henriksson, DICE
- damian
- Member
- 402 posts
- Joined: March 2013
- Offline
- kahuna031
- Member
- 897 posts
- Joined: July 2018
- Offline
-
- Quick Links