Search Unity

How to "parent" an ECS component and access MonoBehavior variables from ComponentSystem::OnUpdate()

Discussion in 'Data Oriented Technology Stack' started by DennisWardAltair, Aug 20, 2019.

  1. DennisWardAltair

    DennisWardAltair

    Joined:
    Apr 4, 2019
    Posts:
    8
    I have a monobehavior script I attach to an empty game object which creates many entities. They are animating perfectly, but are doing so in world space.

    I'd like to somehow parent these entities so they are transformed with respect to a parent gameobject. I'd also like to access variables in the parent gameobject in my ComponentSystem::OnUpdate() so I can affect the speed of animation and scale of each entity.

    I'm hoping to find an efficient way of doing this

    Thanks, Dennis
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    417
    Make a parent Entity synced to your GameObject's transform and make all the new entities children of it.
     
  3. DennisWardAltair

    DennisWardAltair

    Joined:
    Apr 4, 2019
    Posts:
    8
    That's exactly what I want to do, I'm just not sure how to do it. I'm instantiating entities at runtime, and have a parent game object, but I'm not sure how to set the parent on each entity such that it's transform is pushed before my entity transforms are, I'd also like to access the parent object within my system to get values from variables stored within the parent (animation speed, etc.).

    Any help is appreciated!

    Thanks, Dennis
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    417
    The parent should be an entity, not a GameObject. You can make the GameObject an Entity using ConvertToEntity with injection if that makes things easier for you.
     
  5. DennisWardAltair

    DennisWardAltair

    Joined:
    Apr 4, 2019
    Posts:
    8
    Ok - If I have an entity that I've made from a game object, and it has a transform, and I have other entities I've spawned that I want to "add" as children to this parent entity, I'm not sure what API's are available for me to update the child component based upon their parent? I assume I have to somehow find the parent entity for a child entity, read it's transformation, and apply it to my child's transform?

    How would a system that is updating child entities find a parent entity in order to affect child's transform?

    Thanks, Dennis
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    417
    Antypodish likes this.
  7. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    190
    I wrote my own system that syncs up Transforms with Entity system. It is a ComponentSystem (so it doesn't use threading) and has a Dictionary of Entity, Transforms. Then use Entities.ForEach to update it either way during the round. Works quite well.

    I did get rid of Position, Rotation and Scale and just use LocalToWorld and LocalToParent. There are systems for updating the Position and Rotation into LocalToWorld have a lag so I just got rid of them. You can write helpers to convert position and rotation pretty easily so they aren't needed.
     
  8. DennisWardAltair

    DennisWardAltair

    Joined:
    Apr 4, 2019
    Posts:
    8
    Thanks for letting me know about this. I really would like to understand how to create the parent-child relationship, and apply a transformation to the parent (a GameObject, not an entity), and have it's transformation cascade down to 1 entity it somehow owns (the root of an entity subgraph). So I have these things I'm trying to figure out:
    1. how to apply a transform from a parent GameObject to an entity parent box.
    2. how to define the parent-child relation to many child spheres the parent box.
    3. I want to apply transforms only to the parent box and the child spheres should just follow along.
    The approach I am going to try to take is to make a parent game object, and have it spawn the root box. I think I need to define the parent box as a child GameObject and attach a "Game Object Entity" script to it? I then need to get this entity from the game object and then spawn child spheres and define the parent one each sphere entity. I read somewhere that to properly do this, you need to define (on the child entity):
    • Parent
    • LocalToWorld
    • LocalToParent
    If I find out how to properly do this, I'll post it.

    Thanks, Dennis

    By the way, I did try something similar to what you did by placing a gameobject transform in a system and applying that transform to each entity in the job, but this was a workaround that I wasn't happy with.
     
  9. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    190
    Oh that is pretty simple. Just add those three components to whatever child entities you have. Now move the parent entity and all the other entities will be moved at the end of the simulation step.

    You need an entity that represents the GameObject. Then a system that updates the parent root entity from the GameObject transform and all the child information will automatically be updated. There are some components that will also automatically do this that Unity has created as well, but I made my own for my own control.