Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Best way to get object position to a JobComponentSystem?

Discussion in 'Entity Component System' started by fadeway, Sep 30, 2018.

  1. fadeway

    fadeway

    Joined:
    Apr 15, 2018
    Posts:
    9
    Hi,
    I am writing an RTS and have decided to rewrite the visibility code as an ECS system to try out the new paradigm. I wrote a Vision component, the OnUpdate and Execute methods are getting called fine. However I can't figure out how to get access to the transform.

    I found out about the existence of the Position component and added it to my job mask. In my unit factory I naively wrote this code:

    Code (CSharp):
    1.  
    2.             unitBehaviour.Entity = _session.EntityManager.CreateEntity();
    3.             _session.EntityManager
    4.                 .AddComponentData(unitBehaviour.Entity, new Vision());
    5.             _session.EntityManager
    6.                 .AddComponentData(unitBehaviour.Entity, new Position());
    Predictably the resulting Position component is always 0.

    So that unitBehaviour.Entity member is a thing I slapped onto my Behaviour class to store my Vision components with. Then I noticed that gameObjects already come with their own entity and I don't really need to add my own. Perhaps that one has a Position?

    Code (CSharp):
    1.  
    2.             var e = unitBehaviour.gameObject.GetComponent<GameObjectEntity>();
    3.             _session.EntityManager.AddComponentData(e.Entity, new Vision());
    This second attempt fails because e is null. So the GameObjectEntity is not getting automatically created and does not hold the synchronized Position component that I am looking for.

    How do I get a Position that is in synch with the transform.position of the respective gameObject without rewriting my movement system in ECS? Should I just create my own Positions for each gameObject and manually synch them?

    (Unity 2018.2)
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    First of all - in hybrid approach you must add GameObjectEntity manually. Second - for sync positions and transforms use one of:
    • First init Transform from GO to Position - Copy Initial Transform From Game Object
    • Transform from GO to Position - Copy Transform From Game Object
    • From Position to Transform - Copy Transform To Game Object
    (except position they also copy rotation)
     
    fadeway likes this.
  3. fadeway

    fadeway

    Joined:
    Apr 15, 2018
    Posts:
    9
    Thanks!