One common thing I used to do with MonoBehaviour prefabs, was to create an empty object and then add all kinds of things to that. All those objects had positions relative to that of the parent object. That meant that moving or rotating the parent object, moved and rotated all the children correctly and relative to each other.
In ECS, not so much. I have say an empty object, with a capsule attached to it, like so;
But, as soon as I try to convert and instantiate this for ECS, the link between child and parent is broken. Rotating or even moving the empty Beam object, does nothing for the children. I understand that the relation is not as it used to be. But is there a way or proper substitute to convert and use the children as I did before?
Ie, moving the parent also moves the children? And rotating the parent rotates the children? Because converting and the instantiating like this;
Entity beam = manager.Instantiate(beamPrefab.Prefab);
manager.SetComponentData(beam, new Translation { Value = new float3(10,0,0) });
Doesn’t work as I’d expect. It places the Beam empty at 10,0,0 but the capsule with white beam at 0,0,0.
Unity physics doesn’t respect the builtin TransformSystem transformation hierarchies if that’s what your trying to do. You will need to create your own systems to inform the “child” entity’s transform of its relationship to the “parent” if you want them to function like a traditional hierarchy but with physics on one or the other.
More specifically, Unity.Physics will trash your hierarchy if you have a child collider that is considered not part of a compound collider. I’m not quite sure why. It is not a difficult algorithm to extract and inverse apply the world transform to from and to the hierarchy. I wouldn’t blame them for not bothering with the Maya transforms though.
We were also struggling quite a bit with how Unity Physics trashed our hierarchies. Our solution was to put a PhysicsBodyAuthoring component on the outermost parent in the gameobject hierarchy, and only PhysicsShapes on the inner children. The main issue for us was for gameobjects with nested prefabs that each had their own collider hierarchies, which I’m not 100% sure how we ended up handling (or if we’ve even solved it properly so far).
The thing is, I am not really using that hierarchy. Nor does the object in fact have any physics. That being said, I removed the collider, that wasn’t being used anyways. And now it does seem to “work”.