Search Unity

Can't Move Object with LocalToWorld

Discussion in 'Entity Component System' started by PhuongND, Mar 29, 2019.

  1. PhuongND

    PhuongND

    Joined:
    Dec 30, 2013
    Posts:
    30
    I want to move object to position(0,0,0) with LocalToWorld but dont know why my object wasn't moving at all.
    Code (CSharp):
    1. [UpdateInGroup(typeof(SimulationSystemGroup))]
    2.     [UpdateBefore(typeof(TransformSystemGroup))]
    3.     public class MoveCubeSystem : JobComponentSystem
    4.     {
    5.         private ComponentGroup m_UnitGroup;
    6.  
    7.  
    8. //        [BurstCompile]
    9.         public struct MoveUnitJob : IJobProcessComponentDataWithEntity<LocalToWorld, MovementComponentData>
    10.         {
    11.             public float dt;
    12.  
    13.             public void Execute(Entity entity, int index, ref LocalToWorld localToWorld, [ReadOnly] ref MovementComponentData movementComponentData)
    14.             {
    15. //                var currentForward = localToWorld.Forward;
    16.                 var currentPosition = localToWorld.Position;
    17.                 var directionToDestination = movementComponentData.Destination - currentPosition;
    18.  
    19.                 //no turn rate
    20.                 var nextHeading = math.normalizesafe(directionToDestination);
    21.  
    22.                 localToWorld = new LocalToWorld
    23.                 {
    24.                     Value = float4x4.TRS(
    25.                         new float3(localToWorld.Position + (nextHeading * movementComponentData.speed * dt)),
    26.                         quaternion.LookRotationSafe(nextHeading, math.up()),
    27.                         new float3(1.0f, 1.0f, 1.0f))
    28.                 };
    29.  
    30.                 Debug.Log("Index : " + index + " currentPosition :" + currentPosition + " --- directionToDestination: " + nextHeading +
    31.                           " NextPostion :" + localToWorld.Position + " NextHeading :" + localToWorld.Forward);
    32.             }
    33.         }
    34.  
    35.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    36.         {
    37.             var job = new MoveUnitJob()
    38.             {
    39.                 dt = Time.deltaTime
    40.             };
    41.  
    42.             var moveCubeHandle = job.ScheduleGroup(m_UnitGroup, inputDeps);
    43.             inputDeps = moveCubeHandle;
    44.  
    45.             m_UnitGroup.AddDependency(inputDeps);
    46.  
    47.             return inputDeps;
    48.         }
    49.  
    50.         protected override void OnCreateManager()
    51.         {
    52.             m_UnitGroup = GetComponentGroup(new EntityArchetypeQuery
    53.             {
    54.                 All = new[] {
    55.                     ComponentType.ReadWrite<LocalToWorld>(),
    56.                     ComponentType.ReadOnly<MovementComponentData>()
    57.                 }
    58.             });
    59.         }
    60.     }
    There anything that I missed or doing wrong ?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    You update BEFORE TransformSystemGroup which generates LocalToWorld matrix from
    Code (CSharp):
    1. // LocalToWorld = Translation * Rotation * NonUniformScale
    2. // (or) LocalToWorld = Translation * CompositeRotation * NonUniformScale
    3. // (or) LocalToWorld = Translation * Rotation * Scale
    4. // (or) LocalToWorld = Translation * CompositeRotation * Scale
    5. // (or) LocalToWorld = Translation * Rotation * CompositeScale
    6. // (or) LocalToWorld = Translation * CompositeRotation * CompositeScale
    in TRSToLocalToWorld job in TRSToLocalToWorldSystem, which mean all what you doing before - rewrited by transform system. If you want move object you should edit Translation component associated with entity.
     
    PhuongND likes this.
  3. PhuongND

    PhuongND

    Joined:
    Dec 30, 2013
    Posts:
    30
    I got it, thank you!!!
     
  4. PhuongND

    PhuongND

    Joined:
    Dec 30, 2013
    Posts:
    30
    I have a Object has 2 parts : Body + Eye. I notice that the EndFrameParentSystem take alot CPU time than I expected. And I think it's quite weird, Do you have any ideal?
    tải xuống (1).png tải xuống.png
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    One question (not related on your last question). You enabled GPU Instancing on material?
     
  6. PhuongND

    PhuongND

    Joined:
    Dec 30, 2013
    Posts:
    30
    Sorry about that, I dont want to spam on Forum so I ask you in this topic, should I make a new topic ?
    Yes, my material has been enable GPU Instacing, but I turn it off then the result is the same, EndFrameParentSystem still take alot of CPU time.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    And it’s shouldn’t :) It was side question as I told in previous message :)
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Burst enabled?
     
  9. PhuongND

    PhuongND

    Joined:
    Dec 30, 2013
    Posts:
    30
  10. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    I say not about your code, but about in general. Because transform system uses burst compiler too. And also don’t forget about safety checks which editor only anf will be removed in build. I recommend you profile not in editor, but in build.
     
  11. PhuongND

    PhuongND

    Joined:
    Dec 30, 2013
    Posts:
    30
    I think I found what issue is : I enable Leak Detection : Full stack traces
    Screenshot_2.png
    I turn it off then issue has gone
     
  12. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Ah, I even not think about that, because I expected you checked it in first place:D
     
    PhuongND likes this.
  13. sharpwind11

    sharpwind11

    Joined:
    Jun 13, 2019
    Posts:
    9
    I just run through the ECS boid example, it seems that the BoidSystem is also updating the LocalToWorld component before the TRSToLocalToWorldSystem.How did it work?
     
  14. sharpwind11

    sharpwind11

    Joined:
    Jun 13, 2019
    Posts:
    9
    I just found an [WriteGroup(typeof(LocalToWorld))] Attribute on the Boid Component, it can exclude other systems from operating the certain Component.That's why it works.
     
  15. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Yep is legit solution for overriding EntityQuery.