Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

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

  1. toomasio


    Nov 19, 2013

    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 is more apparent on vertical an "elevator"...also becomes more apparent if I step frame by frame in the editor vs just playing.


    Code (CSharp):
    1. protected override void OnUpdate()
    2.         {
    3.             var ecb = buffer.CreateCommandBuffer().AsParallelWriter();
    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 ( != 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.                         }
    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) });
    28.                     }
    29.                     if (count == 0)
    30.                     {
    31.                         if (stick.sticking)
    32.                             stick.sticking = false;
    33.                     }
    35.                 })
    36.                 .Schedule(Dependency);
    38.             buffer.AddJobHandleForProducer(Dependency);
    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


    Apr 6, 2021
    What I can suggest you that take a look at the Unity Physics Samples Character controller demo here: 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.