Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can i modify child local position from parent entity?

Discussion in 'Entity Component System' started by TANGhz17, Mar 21, 2023.

  1. TANGhz17

    TANGhz17

    Joined:
    Jul 20, 2022
    Posts:
    3
    My project needs to run the custom Aspect method in IJob, but when modifying the localPosition of the child entity in this method, the attribute is reported as [ReadOnly].
    Is there any way to work around it?
     
    Last edited: Mar 21, 2023
  2. azmi_unity

    azmi_unity

    Joined:
    Dec 13, 2020
    Posts:
    61
    Perhaps show some code. But generally I'd use an ECB to update components of other entities.
     
  3. TANGhz17

    TANGhz17

    Joined:
    Jul 20, 2022
    Posts:
    3
    My code architecture looks like this:

    -ISystem
    -OnUpdata
    -new IJob Run​

    -IJob
    -Execute(MyAspect myAspect)
    -myAspect.Move​

    -MyAspect
    -private readonly DynamicBuffer<Child> _childBuffer;
    -public NativeArray<Child> ChildArray => _childBuffer.ToNativeArray(Allocator.Temp);
    -void Move
    Code (CSharp):
    1. var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    2. var childArray = ChildArray;
    3. for (var i = 1; i < childArray.Length; i++){
    4. var child = entityManager.GetComponentData<MyChild>(childArray.Value);
    5. entityManager.SetComponentData(childArray.Value, new LocalTransform {
    6. Position = new float3(0, 0, child.Progress) });

     
    Last edited: Mar 22, 2023
  4. TANGhz17

    TANGhz17

    Joined:
    Jul 20, 2022
    Posts:
    3
    The "ECB" you are talking about is the EntityCommandBuffer?
    This is a new concept for me, I just started using it yesterday