How to parent an entity while maintaining world position

I am trying to parent an entity in ISystem as follows:

ecb.AddComponent(selectedAspect.Entity, new Parent { Value = parentEntity });

This results in the child entity getting teleported to a new world position, which I assume to be due to the non-changing local position. Therefore, I tried to modify the local position using the following:

transformAspect.TransformPointWorldToLocal(transformAspect.WorldPosition)

Which, however, is always returning float3(0,0,0)

Not tested, but try somphing like this:

var local = parentTransformAspect.TransformPointWorldToLocal(childTransformAspect.WorldPosition);
ecb.AddComponent(childEntity, new Parent { Value = parentEntity });
ecb.SetComponent(childEntity, new LocalTransform { Position = local /** scale and rotation **/ });
2 Likes

Thanks, that solved it.