Search code examples
c#unity-game-enginegame-physics

Unity3D configurable joint trouble


I need to set up configurable joint like hinge joint, that should be like a metal beam that connect player to object, but trouble is that the player not immidiately stop when i set the anchor point and limit the axis it fly forward to object and then fall to limit, so my beam change his size from shorter to longer and it is bad. Can you help me?

Here the code snippet:

playerJoint.connectedAnchor = anchor.transform.position;
SoftJointLimit limit = new SoftJointLimit();
limit.limit = Vector3.Distance(anchor.transform.position, transform.position);
playerJoint.linearLimit = limit;
playerJoint.yMotion = ConfigurableJointMotion.Limited;
playerJoint.xMotion = ConfigurableJointMotion.Limited;

And then when player unpress the button the follow code is run:

playerJoint.yMotion = ConfigurableJointMotion.Free;
playerJoint.xMotion = ConfigurableJointMotion.Free;

Thanks you in advance.


Solution

  • This sounds a lot like a Rigidbody.interpolation problem.

    In case that it's not, you should try manually changing the player's position to be the length you want. Something like:

    Vector3 hinge2Player = player.transform.position - anchor.transform.position;
    hinge2Player = Vector3.ClampMagnitude(hinge2Player, /* [distance you want] */);
    player.transform.position = hinge2Player + anchor.transform.position;
    

    There's also a small chance that this will help, since it seems like you're trying to do something similar.