Search Unity

Question Setting Entity Parent and Following Parent

Discussion in 'Entity Component System' started by flyQuixote, Sep 28, 2020.

  1. flyQuixote

    flyQuixote

    Joined:
    Jun 23, 2013
    Posts:
    25
    I've been working with dots for a few months and I've been a bit confused at how to handle parenting. I'm trying to add a moving platform that will carry the player entity along with it. As of right now I'm having a difficult time getting it to work. The general code I'm using is this

    Code (CSharp):
    1.  
    2. float deltaTime = Time.DeltaTime;
    3. var commandBuffer = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>().CreateCommandBuffer().AsParallelWriter();
    4.  
    5. // Only applies to grounded KCC characters with a KCC velocity.
    6. Entities.ForEach((
    7.     Entity entity,
    8.     int entityInQueryIndex,
    9.     ref KCCVelocity velocity,
    10.     ref Translation translation,
    11.     in KCCGrounded grounded) =>
    12.     {
    13.         // Displacement of floor
    14.         float3 displacement = float3.zero;
    15.  
    16.         // Bit jittery but this could probably be fixed by smoothing the movement a bit
    17.         // to handle server lag and difference between positions
    18.         if (!grounded.Falling)
    19.         {
    20.             if (this.HasComponent<MovementTracking>(grounded.hitEntity))
    21.             {
    22.                 MovementTracking track = this.GetComponent<MovementTracking>(grounded.hitEntity);
    23.                 displacement = MovementTracking.GetDisplacementAtPoint(track, grounded.groundedPoint);
    24.                 // translation.Value += displacement;
    25.                 if (HasComponent<Parent>(entity))
    26.                     commandBuffer.SetComponent<Parent>(entityInQueryIndex, entity, new Parent { Value = grounded.hitEntity });
    27.                 else
    28.                     commandBuffer.AddComponent<Parent>(entityInQueryIndex, entity, new Parent { Value = grounded.hitEntity });
    29.                 if (HasComponent<LocalToParent>(entity))
    30.                     commandBuffer.SetComponent<LocalToParent>(entityInQueryIndex, entity, new LocalToParent() );
    31.                 else
    32.                     commandBuffer.AddComponent<LocalToParent>(entityInQueryIndex, entity, new LocalToParent() );
    33.             }
    34.             else
    35.             {
    36.                 if (HasComponent<Parent>(entity))
    37.                     commandBuffer.RemoveComponent<Parent>(entityInQueryIndex, entity);
    38.                 if (HasComponent<LocalToParent>(entity))
    39.                     commandBuffer.RemoveComponent<LocalToParent>(entityInQueryIndex, entity);
    40.             }
    41.             velocity.worldVelocity = float3.zero;
    42.         }
    43.         // If was grounded previous frame and no longer falling, add previous floor velocity to
    44.         //  current world velocity as if you jump off with that momentum
    45.         else if (grounded.Falling && !grounded.PreviousFalling)
    46.         {
    47.             velocity.worldVelocity += velocity.floorVelocity;
    48.             if (HasComponent<Parent>(entity))
    49.                 commandBuffer.RemoveComponent<Parent>(entityInQueryIndex, entity);
    50.             if (HasComponent<LocalToParent>(entity))
    51.                 commandBuffer.RemoveComponent<LocalToParent>(entityInQueryIndex, entity);
    52.         }
    53.  
    54.         // Set velocity of floor
    55.         velocity.floorVelocity = displacement / deltaTime;
    56.     }
    57. ).ScheduleParallel();
    58.  
    59.  

    I know the code isn't correct right now but I'm curious if this is even possible. I essentially want the KCC to move with the ground but only translate. I wrote a few custom components to track how an object moves/rotates between frames and a custom component to track what the player is currently standing on. I'm not certain if this is possible but the character seems to disappear whenever they stand on the object. I think the LocalToParent isn't properly initialized but I'm not sure how to fix this error and couldn't find resources that worked on this forum. Probably due to some update in ECS.