What is the best way to keep a component containing Translation & Rotation in sync with GameObject?

  • A GameObject can be moved around by the user during run time
  • I would like to keep an IComponentData in sync with that GameObject’s Position and Rotation (Transform)
  • I found that ConvertToEntity does not keep the Entity data in sync with the GameObject that it converted from
  • I was thinking of putting a reference to the parent GameObject in a component, and then finding the transform of the referred GameObject but I am not sure if looking that up is computationally expensive
  • I could have the GameObject run a script that detects a change of location, and if it is changed, to pull all IComponentDatas that are tracking GameObjects and update the IComponentData tracking its Transform

What is the best way to keep an IComponent data in sync with a move-able GameObject?

1 Like

Great question!

I have poor knowledge of hybrid workflows, so maybe there’s already pre-made stuff that can help you with this. But if not, I’d suggest:

  • Create an Entity with these components: Translation, Rotation, GameObjectLink
  • GameObjectLink would be a custom component you’ve made, which holds a ref to a gameobject. It would be a “class-based” IComponentData, meaning it can hold managed types and you’d define it like this:
public class GameObjectLink : IComponentData
{
    public GameObject GameObject;
}
  • Have a GameObjectLinkSystem that, in its update, will do a job like this:
Entities
.WithoutBurst()
.ForEach((ref Translation translation, ref Rotation rotation, in GameObjectLink gameobjectLink) =>
{
      translation.value = gameobjectLink.GameObject.transform.position;
      rotation.value = gameobjectLink.GameObject.transform.rotation;
}).Run()

Now you just have to assign a gameObject to an Entity’s “gameobjectLink.GameObject”, and that entity will copy the target GO’s transform

This rocks and you rock, THANK YOU!

There’s actually quite a few ways to do this. The real question is how many things do you need to keep in sync? The solutions proposed so far are simple which makes them good for a small amount of transforms. But for larger counts, there are techniques to fully utilize jobs and Burst.

It could be 20-100 parent objects that I need their translation/rotation

Once you start getting closer to 100 or more, it may be worth using jobs and Burst. The key is to maintain a mapping of array indices to entities. Each index corresponds to a packed array of Transforms, specifically a TransformAccessArray. Then you can use IJobParallelForTransform and your mapping to do your synchronization.

I use this for cameras to avoid completing transform jobs and keep my job chain going. I also use this in an animation experiment I am working on where I have several invisible IK driven characters and I have a few thousand entities blending between two of the GameObject poses each frame.

1 Like

Use IJobParallelForTransform;

Here’s an example how to use it for sync’ing (can go both ways):

1 Like

LIke always you rock, thank you for this direction!

Really appreciate this @VergilUa ! I think this will do what I need!