I’d like to have a parent game object and inside I’d like to have child object.
The parent object should only have properly sized collider (based on the mesh in the child object), rigidbody etc but no Mesh filter and MeshRenderer.
The child object should only have the mesh filter and mesh renderer.
Using this solution I will be able to rotate the mesh visually only (not influencing the collider and rigidbody in parent object at all).
Do you guys know how to create this parenting in Blender? Is there any way to achieve this?
I don’t want to do the parenting manually in Unity.
I’m able to achieve this by duplicating the whole sub object, parenting the duplicated object under the original object in Blender. Of course this is not ideal since there are now 2 overlapping objects. And also I need to repeat this process for dozens of objects (separate conveyor belt cylinders). Array modifier will do the thing but still not ideal.
I can always remove the extra mesh renderer and filter in unity keeping the properly sized collider but of course if there is a better way, I’d be glad to use it.
If anybody is interested, here is the heavy lifting script. Put it to editor folder.
Select all desired game objects in hierarchy. Then from Menu select Create/Parenting
After procerssing all these selected game objects will contain child gameobjects with meshRenderer and meshFilter components only (moved from the original selected game objects).
using UnityEditor;
using UnityEngine;
public class Parenting : Editor
{
[MenuItem("Create/Parenting")]
static void CreateChildObjectsWithMeshComponentsFromParents()
{
foreach (var gameobject in Selection.gameObjects)
{
Transform newObject = CreateNewGameObjectInSelectedParent(gameobject.transform);
MoveMeshComponentsToNewObject(newObject);
ResetTransformOfNewObject(newObject);
}
}
private static Transform CreateNewGameObjectInSelectedParent(Transform parent)
{
var child = new GameObject(parent.name + "_mesh").transform;
child.SetParent(parent);
return child;
}
private static void MoveMeshComponentsToNewObject(Transform newObject)
{
var parent = newObject.parent;
var meshComponents = new Component[] { parent.GetComponent<MeshFilter>(), parent.GetComponent<MeshRenderer>() };
foreach (var meshComponent in meshComponents)
{
UnityEditorInternal.ComponentUtility.CopyComponent(meshComponent);
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(newObject.gameObject);
DestroyImmediate(meshComponent);
}
}
private static void ResetTransformOfNewObject(Transform newObject)
{
newObject.localPosition = Vector3.zero;
newObject.localRotation = Quaternion.identity;
}
}