Search Unity

Question How to parent an entity while maintaining world position

Discussion in 'Entity Component System' started by Holyren, Dec 6, 2022.

  1. Holyren

    Holyren

    Joined:
    Jul 8, 2017
    Posts:
    35
    I am trying to parent an entity in ISystem as follows:

    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. transformAspect.TransformPointWorldToLocal(transformAspect.WorldPosition)
    Which, however, is always returning float3(0,0,0)
     
  2. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    792
    Not tested, but try somphing like this:
    Code (CSharp):
    1.  
    2. var local = parentTransformAspect.TransformPointWorldToLocal(childTransformAspect.WorldPosition);
    3. ecb.AddComponent(childEntity, new Parent { Value = parentEntity });
    4. ecb.SetComponent(childEntity, new LocalTransform { Position = local /** scale and rotation **/ });
    5.  
     
    toomasio and ThatDan123 like this.
  3. Holyren

    Holyren

    Joined:
    Jul 8, 2017
    Posts:
    35
    Thanks, that solved it.