Search Unity

Question Transform World Position to Local Position in Parent

Discussion in 'Entity Component System' started by andreyakladov, Apr 16, 2023.

  1. andreyakladov

    andreyakladov

    Joined:
    Nov 11, 2016
    Posts:
    29
    Given:
    World Position and Rotation from RaycastHit. I need to attach instantiated entity to a parent (hit.Entity).
    How to transform world position (hit.Position) and rotation (hit.SurfaceNormal) to parents' ones?

    Without parenting it works just fine, the code below:

    Code (CSharp):
    1. if (!collisionWorld.CastRay(input, out var hit)) continue;
    2. if (manager.HasComponent<HitEffectsData>(projectileEntity))
    3. {
    4. var hitEffects = manager.GetComponentData<HitEffectsData>(projectileEntity);
    5. var effect = ecb.Instantiate(hitEffects.HitPrefab);
    6. ecb.SetComponent(effect,
    7. new LocalTransform
    8. {
    9. Position = hit.Position,
    10. Rotation = quaternion.LookRotation(hit.SurfaceNormal, math.up()),
    11. Scale = 1
    12. });
    13. }
    I've tried to do something like, but position is completely off:
    Code (CSharp):
    1. var localToWorld = manager.GetComponentData<LocalToWorld>(hit.Entity);
    2. ecb.SetComponent(
    3. effect,
    4. new LocalTransform
    5. {
    6. Position = localToWorld.Value.TransformPoint(hit.Position + hit.SurfaceNormal * 0.01f),
    7. Rotation = localToWorld.Value.TransformRotation(quaternion.LookRotation(hit.SurfaceNormal, math.up())),
    8. Scale = 1
    9. });
    10. ecb.AddComponent<Parent>(effect);
    11. ecb.SetComponent(effect, new Parent { Value = hit.Entity});
    So far it seems like the very trivial thing "Translate a position from World to Entity's space" I'm trying to achieve is too complicated for the beautiful new Unity.Transfroms, right?
     
    Last edited: Apr 16, 2023
  2. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    That is expected. Use ComputeWorldTransformMatrix. TransformPoint is relative to parent.
     
  3. andreyakladov

    andreyakladov

    Joined:
    Nov 11, 2016
    Posts:
    29
    Thanks for the reply!
    Found the doc, have a look: https://docs.unity3d.com/Packages/c...orms.Helpers.ComputeWorldTransformMatrix.html
    I've read spec like 10 times already. I do not know what kind of language is that. There is no example or any mention anyone uses it ever. Can't even tell if my use case in the list of the use cases they want me to use it, seems not really suitable for my need to simply "Translate a position from World to Entity's space".
     
  4. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    The documentation saying "relatively uncommon cases" is misleading. What is relatively uncommon is the number of entities in your program that will require complex hierarchical transformations. For example most entities will be simple like bullets. You will find that ComputeWorldTransformMatrix will be common in your code base. You can simply decompose the matrix for rotation and position. You may not always need ComputeWorldTransformMatrix it depends.
     
  5. andreyakladov

    andreyakladov

    Joined:
    Nov 11, 2016
    Posts:
    29
    I gave up in the end with a hope that there will be a normal 'TranslateFromWorldTo(LocalTransform)'-like method in future. Will live with effects in the World space for now, not so critical, as there is no way to actually understand how to work with ComputeWorldTransformMatrix due to absolute lack of examples.
     
  6. ashton_dev

    ashton_dev

    Joined:
    Mar 13, 2019
    Posts:
    1
    I was in the same situation for the last couple of days but I did manage to get ComputeWorldTransformMatrix working after lots of pain—yup the lack of examples was not the nicest experience—and running in a Job as well. My situation was exactly the tires of a raycast car like in the use-case description. It's been a while so I'm curious if you got it working as well or found another solution?