2D wrapping a line around a collision object

Hey there Unity peeps.

So, I have this 2d platformer made out of box colliders. I have the player connected to a point by a LineRenderer and a DistanceJoint2D, for use in physics puzzles.

What I want to do is bend this ‘‘rope’’ around objects in the scene, then retract the rope (shorten the distance of the joint), flinging the player around the object. Basically this image:111206-animation2.gif

I know I’ll be making a list of positions and then move the joint anchors around whenever it wraps/unwraps, but I’m not sure how to tell when and where it wraps on something.

What should I be using for this?

Right, so.
How I ended up doing this was by switching all my platform/wrapping around objects from BoxCollider2Ds to PolygonCollider2Ds. Boxes can’t be asked for their corners as they are defined as a center point with height and width, but poly colliders will return a list of Vector2 if you use polycollider.points.
Then, in Update I just sent a raycast from my character to the last point, and asked the object hit for the .points, and compared them to the raycast collision coordinates, and added the closest fit to the list of points.
and to unwrap you calculate the angle of the last three points in the linerender, if it is bigger than 180, snip that last point from the list!

  • Only use polycollider2D for your platforms
  • On update send raycast along rope
  • On collision use .points to get list of corners
  • compare corners to raycast collision point
  • add closest corner to list/linerender/joint/whatever else you need
    I was greatly aided by the tutorial found here: www.raywenderlich.com/171250/make-2d-grappling-hook-game-unity-part-1