Adding LocalToParent resets child rotation

Hi,

there is a problem with parenting during runtime. I’m using the following code:

CommandBuffer.AddComponent(i, childEntity, new Parent { Value = parentEntity });                   
CommandBuffer.AddComponent(i, childEntity, new LocalToParent { });

Setting the parent this way is resetting child rotation (and translation as well). Code is burst compiled.

Why does this happen and how can I fix this?

Thanks

I’ve posted twice about this issue and yet to receive any response. I might have to make a super simple bug repro project and submit a bug report, but it’s not had to reproduce.

Ok, that would be great. It is indeed a nasty bug, my project basically came to a halt because of this. I have to establish parent child relationships in my game all the time.

I hope they can fix this soon or explain, what we are doing wrong here…

When an entity has no parent translation and rotation components they are effectively in world space.
However when an entity has a parent they are in local space.

1 Like

But shouldn’t the LocalToParent matrix be calculated automatically after adding the component? It does not happen in my case.

I’m trying to use just LocalToWorld, and adding Parent and LocalToParent components resets LTW to 0,0,0.

I believe the correct response would be LTW keeps its current value and LTP is set to the difference between the Parent entities LTW and the child entities LTW.

However, LTW and LTP are both zeroed out, which makes absolutely no sense. How could the child have a LTP of 0,0,0 and a LTW of 0,0,0 when the parent is not at 0,0,0 itself?

Also neither value changes while the parent is moving.

In conversion the local rotation / translation are set for you if you have a parent.

if (hasParent)
        {
            DstEntityManager.AddComponentData(entity, new Translation { Value = transform.localPosition });
            DstEntityManager.AddComponentData(entity, new Rotation { Value = transform.localRotation });

            if (transform.localScale != Vector3.one)
                DstEntityManager.AddComponentData(entity, new NonUniformScale { Value = transform.localScale });

            DstEntityManager.AddComponentData(entity, new Parent { Value = GetPrimaryEntity(transform.parent) });
            DstEntityManager.AddComponentData(entity, new LocalToParent());
        }
        else
        {
            DstEntityManager.AddComponentData(entity, new Translation { Value = transform.position });
            DstEntityManager.AddComponentData(entity, new Rotation { Value = transform.rotation });
            if (transform.lossyScale != Vector3.one)
                DstEntityManager.AddComponentData(entity, new NonUniformScale { Value = transform.lossyScale });
        }

I haven’t really played with this but you will probably to need to replicate this behavior yourself. Not saying this is ideal, just how it is at the moment.