Search Unity

Children relative to parent, not quite working

Discussion in 'Entity Component System' started by Reloque, Nov 29, 2019.

  1. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    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;
    upload_2019-11-29_16-30-5.png

    Now rotating that empty, will rotate with the pivot where I want it in this instance, like so;
    upload_2019-11-29_16-30-56.png

    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;

    Code (CSharp):
    1. Entity beam = manager.Instantiate(beamPrefab.Prefab);
    2. 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.
     
  2. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,267
    Do you have Unity.Physics or Havok Physics installed. Those packages do funky things with the hierarchy.
     
  4. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    I do have Unity.Physics yeah. But I do kinda need that for well, the physics.
     
  5. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    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.
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,267
    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.
     
  7. Zec_

    Zec_

    Joined:
    Feb 9, 2017
    Posts:
    148
    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).
     
  8. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    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".