Search Unity

Sync / copy GameObject Position to Entity

Discussion in 'Entity Component System' started by elJoel, May 13, 2020.

  1. elJoel

    elJoel

    Joined:
    Sep 7, 2016
    Posts:
    125
    I am using both GameObjects and Entities in my Project, and sometimes I need the two to interact.

    For example I want to sync the position of a GO to the entity.

    I have found a solution but it seems like it could be done better.

    Here's my current solution using a static field:

    This is the MonoBehaviour I am adding to the GO I want to track.

    Code (CSharp):
    1. public class CopyGameObjectPositionToEntity : MonoBehaviour
    2. {
    3.  
    4.     public static Vector3 StaticTransform;
    5.     private Transform _transform;
    6.  
    7.     void Start()
    8.     {
    9.         _transform = transform;
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         StaticTransform = _transform.position;
    15.     }
    16. }
    17.  
    This System applies the Position to the Entity.
    Code (CSharp):
    1. public class CopyPositionSystem : ComponentSystem
    2. {
    3.     protected override void OnUpdate()
    4.     {
    5.         Entities.WithAll<SyncedEntity>().ForEach((Entity entity, ref Translation pos) =>
    6.             {
    7.                 pos.Value = CopyGameObjectPositionToEntity.StaticTransform;
    8.             });
    9.     }
    10. }
    11.  
    Am I doing this the right way? Is there a better solution?
     
  2. Kmiecis

    Kmiecis

    Joined:
    Oct 11, 2017
    Posts:
    12
    Well. There are few other solutions to try out as i think static variables can not be bursted and well, there can only be one of each.

    So most probably the best solution would be to create entities for each and link them via, SyncTarget component with Entity and just to set one translation value to other translation value.

    If this is not possible and you just have to have GO and Entity, then i think it would be better to have reference to some entity in GO. So if we would want to GO follow an entity, it would be best if we has this enrity cached and set transform.position to its translation. If other way around, then probably it would be best to create dummy entity just to update its translation and link it to target entity so it reads and sets its translation in a job.
     
  3. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    theres a built in Component/System CopyTransformToGameObject & CopyTransformFromGameObject. both use LocalToWorld over Translation and Rotation so you will have to factor that into your planning(you could just copy the systems and make your own which handles translation and rotation)
     
  4. elJoel

    elJoel

    Joined:
    Sep 7, 2016
    Posts:
    125
    It looks like these will be removed:

    CopyTransformFromGameObjectProxy has been deprecated. Please use the new Gamobject-to-entity conversion workflows instead. (RemovedAfter 2020-07-03).

    Thanks, i'll try these hints out.
     
  5. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    proxy workflow will get removed, but the actual components & systems are not marked for removal.