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

Swinging/Ninja Rope

Discussion in 'Scripting' started by PrimalNectar, Feb 11, 2014.

  1. PrimalNectar

    PrimalNectar

    Joined:
    Jan 25, 2014
    Posts:
    1
    I recently needed to implement rope swinging in a project I'm working on and wanted to share how I did it (since there isn't very much talk on this topic).

    $Swinging Screenshot.png

    I'm creating a swing with the Unity Component HingeJoint creating it via code (instead of in the Editor) when the user left-clicks then destroying it when the player releases the button. I'm creating and destroying the Component because I only want the player to be attached when holding down the button and HingeJoints can not be disabled.

    I'm using a Player and Anchor GameObject. The Anchor isn't rendered but is moved to the point where you're swinging from on click. The Anchor NEEDS a HingeJoint too! If only the player has a HingeJoint it will create unexpected behaviors.

    $Player Inspector Screenshot.png
    $Anchor Inspector Screenshot.png

    Code (csharp):
    1.  
    2. /** On left Click **/
    3.  
    4. //move the anchor to the correct position
    5. anchor.transform.position = new Vector3 (hit.point.x, hit.point.y, 0);
    6. //zero out any rotation
    7. anchor.transform.rotation = Quaternion.identity;
    8.  
    9. //Create HingeJoints
    10. joint = gameObject.AddComponent<HingeJoint> ();
    11. joint.axis = Vector3.back; /// (0,0,-1)
    12. joint.anchor = Vector3.zero;
    13. joint.connectedBody = anchor.rigidbody;
    14. anchorJoint = anchor.AddComponent<HingeJoint> ();
    15. anchorJoint.axis = Vector3.back; /// (0,0,-1)
    16. anchorJoint.anchor = Vector3.zero;
    17.  
    18. //Now just add Force!
    19.  
    20. /** On left Click release **/
    21.  
    22. //Destroy HingeJoints
    23. Destroy (joint);
    24. Destroy (anchorJoint);
    25.  
    26.  
    Notes
    • Player input for movement adds force to the Player's Rigidbody in the left and right direction
    • I'm using a LineRenderer and a double faced material (no culling) to draw the line
    • Make sure there aren't any colliders on the Anchor or physics will affect it creating unexpected behavior
    • I have constrains on the Player's Rigidbody because I'm working on 2D and don't care about an axis

    I hope this is helpful, happy Coding! :cool:
     

    Attached Files:

  2. matbrummitt

    matbrummitt

    Joined:
    Jan 12, 2010
    Posts:
    107
    Thanks for sharing. I started work on a ninja roping game last year which unfortunately i've been too busy to complete. I have all the rope wrapping and unwrapping working nicely, and left it halfway through the physics stage. From memory I believe I went for the same joints you did and it was working ok, but not perfectly, so when I get the chance to continue working on it, i'll refer back to this post for guidance.
     
  3. A-man tuna

    A-man tuna

    Joined:
    Dec 2, 2012
    Posts:
    1
    Thanks for sharing your work! However i'm getting a parsing error, is this becuase i'm working in 3d and not 2d?
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Thanks for sharing. Nice snippet.
     
  5. Killinq

    Killinq

    Joined:
    Jul 3, 2018
    Posts:
    1
    Sorry for necro, but does anyone have any idea have the force should be used? Should I use rb.AddForce() or something else? I'm doing this in 3D by the way.