Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object dragging

Discussion in 'Physics' started by Necronomicron, Apr 1, 2019.

  1. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108
    Hello!
    I'm trying to implement dragging of objects with different masses (like in Half-Life 2, Viscera Cleanup Detail, etc.). So far I have a dragger with Rigidbody and ConfigurableJoint on a player's child. It kinda works but it has some flaws.

    So how it should be:
    • It shouldn't rotate midair, but should rotate when it touches surfaces or else if needed.
    • If object clings on something, it should act like it's on spring. I read docs and I tried to play with parameters but had no success. It just psychotically jitters (see video). Before ConfigurableJoint I was using SpringJoint and it was like I want, but problem with SpringJoint was that object was rotating and flying around like crazy.
    • If player drops object while moving or rotating, object should save inertia. With SpringJoint it was like that, now it just falls down (see video).

    I've also added Linear Limit, because without it objects were jittering midair but now objects are weirdly swaying (see video).

    I apply these parameters to ConfigurableJoint:
    Code (CSharp):
    1. cj.autoConfigureConnectedAnchor = false;
    2. cj.anchor = cj.axis = cj.secondaryAxis = Vector3.zero;
    3. cj.connectedAnchor = hit.rigidbody.centerOfMass;
    4. cj.xMotion = cj.yMotion = cj.zMotion = cj.angularXMotion = cj.angularYMotion = cj.angularZMotion = ConfigurableJointMotion.Limited;
    5. SoftJointLimit linearLimit = cj.linearLimit;
    6. linearLimit.limit = draggerLinearLimit;
    7. cj.linearLimit = linearLimit;
    8. cj.breakForce = draggerBreakForce;
    9. cj.connectedBody = hit.rigidbody;
     
  2. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  3. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    Hey there,

    Just try again with the spring joint. When you attach it, set useGravity of the target to false and increase angularDrag on the target, too (remember to set it back to what it was when you release the object). The spring joint can also apply damping, which you should also adjust to your needs.

    Note that the value for “spring” and “damping” of the spring joint need to scale with the mass of the target object.

    You’re on a good way, keep trying!
     
  4. the_0dium

    the_0dium

    Joined:
    Nov 29, 2018
    Posts:
    15
    Yep. Spring joint works fine. I did dragging similar to hl2 myself a few years ago. Also don't forget to deattach dragged object when it's too far from players child ;)
     
  5. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108
    I tried many different values for Spring and Damper, but object is either still flying like crazy or too slow. If I change object's drag, it becomes even weirder.