Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Dynamic parenting issues

Discussion in 'Physics for ECS' started by RBogdy, Dec 27, 2019.

  1. RBogdy


    Mar 6, 2019

    My use case is the following:

    1. I do a ray cast on mouse down (wasPressedThisFrame) checking my physics entity (Physics Body, Physics Shape)
    2. If it's the entity I want, I instantiate a second entity (just Plane Physics Shape) which I dynamically parent to the first entity, by adding the Parent component (Value = 1st entity) and LocalToParent Component

    The problem I'm running into is that the Plane Entity is instantiated at 0,0,0 even thought the link seems to be made and it has it's LocalToWorld updated to be the same as the parents one (when I turn gravity to -0.02 my regular entity slowly raises from the 'floor', the plane entity remains at 0,0,0 even though the LocalToWorld matrix is updated to be the same as for the regular entity).

    What I basically want to achieve is to have my Plane Entity at the same Translation as my regular Entity (Plane is set as trigger and there's no collision between the two as I filter it out).

    As a workaround I did a Job to copy the Translation from the regular entity to the plane entity such as:

    struct PlaneFollowJob : IJobForEachWithEntity<Parent>
    [NativeDisableParallelForRestriction] public ComponentDataFromEntity<Translation> TranslationData;

    public void Execute(Entity entity, int index, [ReadOnly] ref Parent parent)
    TranslationData[entity] = TranslationData[parent.Value];

    This works, however it fells like a hack and I was expecting the already existing Translation System to move the Plane with the regular entity.

    Is there a better way to create this link or should I settle for my method?
    Last edited: Dec 27, 2019
  2. Adam-Mechtley


    Unity Technologies

    Feb 5, 2007
    The issue is that the BuildPhysicsWorld system assumes the value of the Translation component is in world space. As I noted in in this thread, the restriction is being relaxed for static bodies (i.e. something with PhysicsCollider but no PhysicsVelocity), which sounds like your case, in which this approach should "just work". In the meantime though (and ultimately it may be the preferable option anyway), you can just make your "child" in world space and create some kind of Follow component/system to track the "parent" in world space.
    nicolasgramlich likes this.