Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question How to specify Kinematic vs Dynamic rigidbody collision speed

Discussion in '2D' started by DrDemencio, May 21, 2024.

  1. DrDemencio

    DrDemencio

    Joined:
    Sep 2, 2022
    Posts:
    92
    Hi there!

    I'm trying to make a Kinematic rigidbody (which I'm moving using Rigidbody2D.MovePosition) push a Dynamic rigidbody. But, when the Kinematic starts pushing, I want the speed to be reduced to simulate a resistance. I want then both bodies to keep moving together from that point. So it should work like this:
    upload_2024-5-21_11-58-45.png
    If I'm not mistaken, when the Dynamic rigidbody is collided, it inherits the Kinematic rigidbody speed. The problem is if this collison happens in the middle of a frame I want to move with Speed1 until that point and with Speed2 from there. There is no problem on the Kinematic side, as I can adjust the displacement as much as I want, but the speed of the Dynamic rigidbody will be Total Displacement / Time.FixedDeltaTime instead of Speed2. This will cause both objects to separate.

    How could I prevent this?

    Thanks for your time.
     
  2. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    293
    Maybe before moving the kinematic body a step forward, you could cast a ray in the direction of the movement for the length of that step. If a collision is detected, adjust the step length accordingly (to stop at the dynamic body) and also reduce the speed as needed.
     
  3. DrDemencio

    DrDemencio

    Joined:
    Sep 2, 2022
    Posts:
    92
    Well, I can certainly end the frame right before touching the Dynamic rigidbody and then start the next frame with Speed2. Unfortunately this is going to be problematic if Dynamic is not completely stopped (sorry, I haven't specified before but I'm looking for a method that works in a general case). And, even if it's not, there will be a small decrease in Kinematic speed, which could be acceptable but not great.
     
    Last edited: May 21, 2024