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

Enable/Disable a joint?

Discussion in 'Scripting' started by fivearchers, Jun 7, 2009.

Thread Status:
Not open for further replies.
  1. fivearchers

    fivearchers

    Joined:
    Apr 17, 2009
    Posts:
    716
    In my game when you pickup something, the cargoObject becomes the attached body of the main player object. This works pretty good so far.

    The problem is at some point I want to drop the object and disable the joint...but I can't find a way to do that. The enabled property doesn't exist. Setting the attached body to null staples it to the world (hanging in space).

    I'm sure there must be a basic way to do this?
     
  2. fivearchers

    fivearchers

    Joined:
    Apr 17, 2009
    Posts:
    716
    Oh I set the breakForce to zero, and that dropped it off..but that seems to destroy the joint all together. How would I get around that?
     
  3. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    I believe your only option is to destroy the joint and recreate it later (which isn't that bad of a solution, really).
     
  4. fivearchers

    fivearchers

    Joined:
    Apr 17, 2009
    Posts:
    716
    I think that's what I'm going to go with. Create the joint as I pick the object up and destroy it when I let it go. I've added a marker mesh to read when I pick up the object to use as the joint location.
     
  5. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Matthew is correct. I've got the same thing as you in my current project, and destroying + re-creating appears to be the only way.
     
  6. showoff

    showoff

    Joined:
    Apr 28, 2009
    Posts:
    273
    do u mind posting the script at all.
     
  7. miras

    miras

    Joined:
    Jan 4, 2014
    Posts:
    1
    void Update () {
    SpringJoint2D name = GetComponent<SpringJoint2D>();
    name.enabled = false;
    }
     
  8. Konowy

    Konowy

    Joined:
    Jun 3, 2015
    Posts:
    1
    Joints don't have variable enabled.
     
  9. belamessex

    belamessex

    Joined:
    May 12, 2015
    Posts:
    12
    this seems like an old thread, but in case anybody is looking for a quick work around, i did this.... created an empty game object with a rigidbody only. When the joint is active write something like:

    if (springyness) springJoint.connectedBody = MainCharacterRigidbody;
    else springJoint.connectedBody = EmptyObjectRigidbody;

    seems to work like a charm with no hickups. hope that helps!
     
  10. bjennings76

    bjennings76

    Joined:
    Jun 15, 2013
    Posts:
    6
    I just ran into this same problem. I wanted to disable/enable joints without destroying them. Thanks to @belamessex, I was able to create a JointToggler component that finds a joint on the same object and connects/disconnects the connectedBody rigidbody when it is enabled/disabled (like Joints should do by default).

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class JointToggler : MonoBehaviour
    4. {
    5.    [SerializeField] private Joint joint;
    6.    private Rigidbody connectedBody;
    7.  
    8.    private void Awake()
    9.    {
    10.        joint = joint ? joint : GetComponent<Joint>();
    11.        if (joint) connectedBody = joint.connectedBody;
    12.        else Debug.LogError("No joint found.", this);
    13.    }
    14.  
    15.    private void OnEnable() { joint.connectedBody = connectedBody; }
    16.  
    17.    private void OnDisable()
    18.    {
    19.        joint.connectedBody = null;
    20.        connectedBody.WakeUp();
    21.    }
    22. }
     
    Last edited: Feb 16, 2017
  11. g-hoot

    g-hoot

    Joined:
    Apr 18, 2015
    Posts:
    69
    Thanks @belamessex and @CthulhuComplex ! I'm pulling a lever with a Vive Controller and was really disappointed to see that the Spring Joint was destroyed on breaking. You guys got me going without having to recreate the joint.I added a sphere collider on the lever and on the Vive controller, set the break force on the Spring Joint to Infinity and added this code to the lever. Works great!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ConnectControllerSpringJoint : MonoBehaviour
    6. {
    7.     public SphereCollider ControllerSphereCollider;
    8.     public SpringJoint SpringJoint;
    9.     public Rigidbody ControllerRigidBody;
    10.     public Rigidbody EmptyForHandleSpringJoint;
    11.    
    12.     private void OnTriggerEnter(Collider col)
    13.     {
    14.         if(col== ControllerSphereCollider)
    15.         {
    16.             SpringJoint.connectedBody = ControllerRigidBody;
    17.         }
    18.     }
    19.     private void OnTriggerExit(Collider col)
    20.     {
    21.         if (col == ControllerSphereCollider)
    22.         {
    23.             SpringJoint.connectedBody = EmptyForHandleSpringJoint;
    24.         }
    25.     }
    26. }
     
  12. PPDeluXe

    PPDeluXe

    Joined:
    Feb 23, 2016
    Posts:
    2
    I was able to enable/disable the (2D spring) joint using:
    Code (CSharp):
    1. GetComponent<SpringJoint2D> ().enabled = false;
     
  13. talyh

    talyh

    Joined:
    Dec 2, 2016
    Posts:
    20
    For anyone looking into this. I was just struggling with this and didn't want to have extra Rigidbodies or extra creation/destructions.

    Not sure if it'd work in any scenario, but, in my case, turning the "spring" all the way to 0 while there's nothing connected and turning it up when there's a body connected was sufficient to mimc the "enabled/disabled".
     
    BeefyMcWhatnow, QQQ_QQQ, XCO and 2 others like this.
  14. deweypottsLANL

    deweypottsLANL

    Joined:
    Jul 5, 2018
    Posts:
    1
    joint.connectedBody is giving me issues (the joint is underlined in red). Any ideas?
     
  15. Miracle2042

    Miracle2042

    Joined:
    Sep 1, 2019
    Posts:
    1
    can you tell me how to do same thing in 3d spring joint
     
  16. QQQ_QQQ

    QQQ_QQQ

    Joined:
    Jul 12, 2019
    Posts:
    15
    Thanks:) Simple yet effective
     
  17. michealcaj

    michealcaj

    Joined:
    Aug 18, 2017
    Posts:
    191
    Won't work
     
  18. wulcat

    wulcat

    Joined:
    Aug 19, 2019
    Posts:
    5
    The best way I found is to turn on the limits and set the angle to restrict any hinge movement.

    Code (CSharp):
    1. public JointLimits joinLimit;
    2. public void Activate()
    3. {
    4.      hingeJoint.limits = joinLimit;
    5. }
    6.  
    7. public void Deactivate()
    8. {
    9.      hingeJoint.limits = new JointLimits();
    10. }
     
  19. immeasurability

    immeasurability

    Joined:
    Apr 22, 2014
    Posts:
    125
    2022 Year this problem still exist... Full code for enable and disable joint.

    Code (CSharp):
    1. private ConfigurableJointMotion      _data_motion_x;
    2. private ConfigurableJointMotion      _data_motion_y;
    3. private ConfigurableJointMotion      _data_motion_z;
    4.  
    5. private ConfigurableJointMotion      _data_motion_x_angular;
    6. private ConfigurableJointMotion      _data_motion_y_angular;
    7. private ConfigurableJointMotion      _data_motion_z_angular;
    8.  
    9. private Vector3                      _joint_anchor = Vector3.zero;
    10. private Vector3                      _joint_anchor_connected = Vector3.zero;
    11.  
    12.  
    13. public ConfigurableJoint            _joint;
    14.  
    15.  
    16. private void Awake() {
    17.  
    18.     this._joint.autoConfigureConnectedAnchor = false;
    19.  
    20.     this._joint_anchor = this._joint.anchor;
    21.     this._joint_anchor_connected = this._joint.connectedAnchor;
    22.  
    23.     this._data_motion_x = this._joint.xMotion;
    24.     this._data_motion_y = this._joint.yMotion;
    25.     this._data_motion_z = this._joint.zMotion;
    26.  
    27.     this._data_motion_x_angular = this._joint.angularXMotion;
    28.     this._data_motion_y_angular = this._joint.angularYMotion;
    29.     this._data_motion_z_angular = this._joint.angularZMotion;
    30. }
    31.  
    32. public void execute_joint_connection_enable_false() {
    33.  
    34.     this._joint.xMotion = ConfigurableJointMotion.Free;
    35.     this._joint.yMotion = ConfigurableJointMotion.Free;
    36.     this._joint.zMotion = ConfigurableJointMotion.Free;
    37.  
    38.     this._joint.angularXMotion = ConfigurableJointMotion.Free;
    39.     this._joint.angularYMotion = ConfigurableJointMotion.Free;
    40.     this._joint.angularZMotion = ConfigurableJointMotion.Free;
    41.  
    42.     //        if this._joint.autoConfigureConnectedAnchor == true
    43.     //        remember last state of anchor
    44.     //    this._joint_anchor = this._joint.anchor;
    45.     //    this._joint_anchor_connected = this._joint.connectedAnchor;
    46. }
    47.  
    48. public void execute_joint_connection_enable_true() {
    49.  
    50.     this._joint.xMotion = this._data_motion_x;
    51.     this._joint.yMotion = this._data_motion_y;
    52.     this._joint.zMotion = this._data_motion_z;
    53.  
    54.     this._joint.angularXMotion = this._data_motion_x_angular;
    55.     this._joint.angularYMotion = this._data_motion_y_angular;
    56.     this._joint.angularZMotion = this._data_motion_z_angular;
    57.  
    58.     //        if this._joint.autoConfigureConnectedAnchor == true
    59.     //    this._joint.anchor = this._joint_anchor;
    60.     //    this._joint.connectedAnchor = this._joint_anchor_connected;
    61. }
    62.  
     
  20. happyfirecn

    happyfirecn

    Joined:
    Dec 28, 2016
    Posts:
    11
    Good! I use the same method, but I don't known if there is any side-effect of that.
     
  21. canmujde

    canmujde

    Joined:
    Oct 16, 2018
    Posts:
    4
    Bro, this saved my life, thanks from 2022. WakeUp() trick in OnDisable() is magic!
     
    boris47Sense likes this.
  22. sillyninja65

    sillyninja65

    Joined:
    Dec 1, 2022
    Posts:
    9
    you aint gonna believe this but this worked for me and im kind of amazed.

    write a script that just says this:

    Public HingeJoint joint;
    Public gameObject body;

    assign the correct hinges and mesh and that fixed it for me.

    disable the mesh, enable it and boom it goes back to normal.

    ill do more tests but it seems simply binding them through that 2 line script actually worked
     
  23. sillyninja65

    sillyninja65

    Joined:
    Dec 1, 2022
    Posts:
    9
    only just noitced this is a 2009 issue but hey if it helps anyone then cool
     
  24. Seldatron

    Seldatron

    Joined:
    Jun 7, 2018
    Posts:
    1
    You could just adjust the mass so when say the door is locked the mass is set to a incredible number and when unlocked the mass is something reasonable to say 1 or 10
     
  25. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    This thread is 14 years old. Please don't necropost.
     
Thread Status:
Not open for further replies.