How to make a parent-child relationship with a GameObject

Hi guys, I am very new to ECS and am currently trying some tutorials.

In a video I am watching to learn, CopyTransformFromGameObjectProxy and some other components like PositionComponent are used to keep a parent-child relationship with a GameObject which is converted to Entity by GameObjectEntity. And I figured out that XXXComponent was renamed to XXXProxy (For example, RotationComponent to RotationProxy), but these components seems to be deprecate. I guess it is because using ConvertToEntity is recommended now instead of GameObjectEntity. But then how can I keep a parent-child relationship with a GameObject?

To explain more details, I have a GameObject which has not-compatible-with-ECS components such as RigidBody and Animator, and a child GameObject , which doesn’t have such components. I want to convert both but keep legacy components like RigidBody. (I want ComponentSystems to change variables or use functions in legacy components.) So, I use GameObjectEntity for the parent now. If I use ConverToEntity for the child, yes, I can convert it, but no relationship with its parent. What should I do?

You are watching an old, outdated tutorial. GameObjectEntity and proxies should not be used anymore.

To be clear are you saying you have a parent gameobject and a child gameobject, you want the parent to stay a gameobject, but if you move the child into DOTS, you won’t be able to tell which gameobject was the parent? Could you use GetPrimaryEntity() during conversion to get the childs entity, then use that as the key in a dictionary that maps from child entity to parent gameobject?

I thought ConvertToEntity can’t deal with MonoBehaviours but gameObjectEntity, and that is why I wanted to use GameObjectEntity. However, I figured out it seems that ConvertToEntity can. I will try the latest workflow. thanks.

I am very new to DOTS and didn’t know the function. And I don’t know really know how to solve the problem even now. What you mean is that I can get the child entity using the function and base on its transform information(I guess Translation, Rotation, and LocalToWorld) I can manually set local position and stuff. Am I correct?

I guess I don’t understand what you’re trying to do, sorry

Sorry, I am not good at English. I will try to explain as much as possible. It is just I have a GameObject with Animator and RigidBody etc and also children of it, and I want to convert all of them to Entities. At first I thought the only way to use MonoBehaviour componets like RigidBody with ECS is to use GameObjectEntity(That is why I wanted the parent to stay GameObject), and then I found ConvertToEntity can too with “ConvertAndInjectGameObject” option, so now I am trying to achieve my goal with it.

What I asked you in the previous reply is about the old workflow, but it seems like it is better to use ConvertToEntity workflow now if I can.

What I am stuck now using the latest workflow is that ConvertToEntity with “ConvertAndInjectGameObject” doesn’t make children Entities automatically. But If I use SubScene or ConvertToEntity with another option, then, MonoBehaviours cannot be used.

Oh I didn’t know ConvertAndInjectGameObject doesn’t make child entities! Even if you also give the child its own ConvertToEntity it doesn’t get created, huh.

Could you remove the ConvertToEntity from the Parent? Then move its children to a new gameobject (“new child” in the pic) and give that a ConvertToEntity with ConvertAndDestroy? So afterward, you still have the parent gameobject with the rigidbody in the monobehaviour world, and all the children have a Parent ECS component in the ECS world.

As you said, I made a middle parent(“NewChild” in the picture) and put ConvertoToEntity with ConvertAndDestroy, but the created entity’s transform is not related to the root parent(“ParentWithRigidBody”) which works with RigidBody.

So heres what I did
During conversion/hybrid mode:

  • Create a Parent monobehaviour (“ParentAuth” in the pic) and put it on the parent gameobject
  • Parent monob has an Entity field named NewChild
  • Create a NewChild monobehaviour and put it on the new child
  • NewChild has ConvertToEntity ConvertAndDestroy
  • Create a GameObjectConversionSystem that looks for Parent monobehaviours and uses GetComponentInChildren() to find the NewChild under the Parent
  • Set the Parent.NewChild to the Entity that was created for NewChild

Then during the game loop:

  • In a ComponentSystem.Update(), FindObjectsOfType()
  • Get the entity that is mirroring the parent gameobject with parent.NewChild
  • Set whatever transforms and rotations you want on the NewChild entity, because you have access to the parent gameobject which is subject to rigibody gravity etc

There is a component called CopyTransformFromGameObject but I couldn’t see how to use it.

I tried it and now NewChild has the same rotation as Parent’s! And if I add EntityManager.SetComponentData(…) in ChildJob, then transform also gets sync. That is great, but I found a problem… Originally, I put GameObjectEntity to the RigidBody parent and in a ComponentSystem which handles movement uses RigidBody.MovePosition and MoveRotation to move the parent.

In this case, I guess it is better to convert the parent to Entity but keep the GameObject so the entity holds RigidBody to move the GameObject, and make a child Entity somehow and sync transform with the parent somehow. I found if I use ConvertToEntity with ConvertAndDestroy, it makes the root and its children entities and put Child to the root and put Parent, LocalToParent, and PreviousParent to the children. So, I think those ComponentDatas are used to make a parent-child relationship, though I don’t know how to use them yet. I will search for a way to set them manually…

About CopyTransformFromGameObject, I think it should be used with ConvertToEntity with ConvertAndInjectGameObject and it updates LocalToWorld ComponentData.