Search Unity

ConvertToEntity Destroy vs Inject

Discussion in 'Entity Component System' started by shotoutgames, Jul 7, 2020.

  1. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    290
    Apologees as I have probably been asking a similar question in many different ways.
    I am still trying to understand all of this.
    Here is a hopefully simplified case.

    I have a parent game object with a Converttoentity component and Applyimpulsecomponent authoring.
    The child object contains the mesh used.

    I move the entity with a simple system. with velocity 0,0,10 to move the entity forward.

    What I don't get?
    Convert to Entity destroy works as expected. The mesh just goes forward in the Z direction.
    But Inject Game Object does nothing.
    The debugger shows the physicsvelocity as 0,0,10 but the translate doesn't change at all.

    I was also using a gameobject tracker script to follow the entity position but this has no effect on or off as the entity doesn't move with inject on.

    The most frustrating parts was when I stopped last night I could swear it worked properly but no luck in the morning?

    I guess I don't understand the difference between the inject and destroy.

    I also researched AddHybridComponent but not sure if this could help me.

    Thanks for ANY input

    Code (CSharp):
    1. protected override void OnUpdate()
    2.     {
    3.         /// Get the physics world.
    4.         Entities.WithoutBurst().ForEach(
    5.             (
    6.                 Entity entity,
    7.                 ref LocalToWorld localToWorld,
    8.                 ref Translation translation,
    9.                 ref Rotation rotation,
    10.                 ref PhysicsVelocity physicsVelocity,
    11.                 in PhysicsMass physicsMass,
    12.                 in ApplyImpulseComponent applyImpulseData
    13.             ) =>
    14.             {
    15.                 physicsVelocity.Linear = new float3(0,0,10);
    16.             }).Run();
    17.     }
     
  2. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    The injected game object does not sync its transform to the Translation Co,mponent which unity physics relies on. You need to sync these, you can look at the CopyTransformToGameObjectSystem for an idea of how to do this in jobs but the easiest would be a main thread for each that just copies translation to transform.position. Note same thing applies for rotation.