Search Unity

Question Dynamic Physics Body on moving platforms. Override collisions possible?

Discussion in 'Physics for ECS' started by toomasio, Apr 11, 2022.

  1. toomasio

    toomasio

    Joined:
    Nov 19, 2013
    Posts:
    199
    Hello,

    I am getting some inconsistent behavior on my stick to platform system. I basically grab the translation "velocity" on the platform entity, and update the translation offset on whatever object sticks to the platform. The problem is, the platform will "collide" with the physics body and push the body sometimes. This happens very inconsistently.

    I tried "overriding" the dynamic body's Local To World position in the EndFixedStepSim buffer, which I thought solved my issue until I noticed a collision still happens sometimes.

    Other than this the sticking works perfectly...it is more apparent on vertical movements...like an "elevator"...also becomes more apparent if I step frame by frame in the editor vs just playing.

    platStick1.jpg
    platStick2.jpg
    platStick3.jpg
    platStickOrder.jpg

    Code (CSharp):
    1. protected override void OnUpdate()
    2.         {
    3.             var ecb = buffer.CreateCommandBuffer().AsParallelWriter();
    4.  
    5.             Dependency = Entities
    6.                 .ForEach((Entity entity, int entityInQueryIndex, ref StickToGround stick, ref Translation trans, in LocalToWorld ltw, in DynamicBuffer<PhysicsHitBuffer> buffer) =>
    7.                 {
    8.                     int count = 0;
    9.                     for (int i = 0; i < buffer.Length; i++)
    10.                     {
    11.                         var hit = buffer[i];
    12.                         if (hit.name != stick.physicsHitName) continue;
    13.                         count++;
    14.                         stick.lastStickyGround = hit.hitData.entity;
    15.                         var tv = GetComponent<TranslationVelocity>(stick.lastStickyGround);
    16.                         if (!stick.sticking)
    17.                         {
    18.                             Debug.Log($"Stick");
    19.                             stick.sticking = true;
    20.                         }
    21.  
    22.                         trans.Value += tv.velocity;
    23.                         //setting the translation is NOT GOOD ENOUGH.
    24.                         //you must set the world position directly in the physics loop
    25.                         //BEFORE the Transform group so collision detections do not "bump" this entity
    26.                         ecb.SetComponent(entityInQueryIndex, entity, new LocalToWorld { Value = float4x4.TRS(ltw.Position + tv.velocity, ltw.Rotation, 1) });
    27.  
    28.                     }
    29.                     if (count == 0)
    30.                     {
    31.                         if (stick.sticking)
    32.                             stick.sticking = false;
    33.                     }
    34.                
    35.                 })
    36.                 .Schedule(Dependency);
    37.  
    38.             buffer.AddJobHandleForProducer(Dependency);
    39.  
    40.         }
    I need a way to override the collision positions so the dynamic body's translation just stays to the relevant position.

    EDIT: Currently trying to just do this in the TransformSystem instead...seems to be working so far. Had to rework some of the ways I did my grounded detection.
     
    Last edited: Apr 11, 2022
  2. DevViktoria

    DevViktoria

    Joined:
    Apr 6, 2021
    Posts:
    94
    What I can suggest you that take a look at the Unity Physics Samples Character controller demo here:
    https://github.com/Unity-Technologi...Assets/Demos/6. Use Cases/CharacterController
    This is a third person character controller that is able to handle perfectly when the character is on a moving platform.
    If I recall correctly the handling of the movement happens in the CharacterControllerAuthoring.cs and in the CharacterControllerUtilities.cs in the Scripts directory.
     
    bb8_1 and toomasio like this.