Search Unity

Making a bouncy line?

Discussion in 'Scripting' started by KyleStank, Dec 13, 2017.

  1. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    Hello,

    I was wondering how to make a LineRenderer "bounce" as it follows the user's mouse. I have tried using a HingeJoint2D. While this adds the bouncy effect (sort of), it creates a "fixed" rope so moving back is impossible. HingeJoint2D might be the solution, I am just not sure how to configure it for this. Watch this video here. The first part shows what I am looking for, and the second part shows this being done with a LineRenderer.

    I am trying to fix this for a client. I feel really stupid right now as I should know how to do this, but I am simply going mind blank. Does anyone have any ideas for this? Please watch the video to get an understanding of what I need.

    Thanks :)
     
  2. TheHighGround

    TheHighGround

    Joined:
    Nov 19, 2017
    Posts:
    68
    Could you not just attach the head and tail to a Spring and it will compress back?
     
  3. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    What do you mean by that exactly? Do you mean use HingeJoint2D and attach a SprintJoint2D somewhere?
     
  4. TheHighGround

    TheHighGround

    Joined:
    Nov 19, 2017
    Posts:
    68
    Yeah the SpingJoint2D should have a Rigidbody field. Drag the tip there.
     
  5. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    He is what I have set up(ignore the random art I used, lol): https://streamable.com/658ol

    It isn't working the way I would like. The HingeJoint like messes up when you go too far out or too close in. The Sprint Joint does bring it back nicely though. My primary goal is to create the "swing" effect with the line though.

    I need this to be exactly like the first video I showed.

    The "rope" is updated as the user's mouse moves and it swings around as the mouse moves. When the mouse click is let go, the "rope" swings back to the origin point. I realize the swinging part can be done with a SwingJoint2D, but I have no idea how to create a swing effect when the user is moving the mouse. Yes, it barely works with HingeJoint2D, but since the rope is being updated in length as the mouse moves, if you go too far out or too close in it glitches out.

    I am clueless...
     
  6. TheHighGround

    TheHighGround

    Joined:
    Nov 19, 2017
    Posts:
    68
    I've never made a rope before but if I had to make one I would start with Bezier Curves/Splines and add the physics Rigidbody on the nodes to give it that rope effect + visual curves.

    You look like you're on the right track just implement the drawing of the Curves and you should be good to go.

    Here's a Blog on Bezier Curves/Splines for Unity.