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:
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?
Answer by Duodecimus · Apr 29, 2018 at 05:37 AM
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!
TL;DR
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
Hiii, Can you please post the solution or the script if you don't $$anonymous$$d?
@bhavinbhai2707 Here you are, though I would recommend following the tutorial I linked in my answer to get a better understanding of what is going on. https://pastebin.com/xKx4h23w
Hii, Thank you so much for replying. I really appreciate it. The link doesn't seem to work. Can you please re-share it? I just need it for the reference point of view to understand how I want to approach my use case.
Your answer
Follow this Question
Related Questions
Sliding animation after running 0 Answers
Why did my character start jumping infinitely 0 Answers
Platform Bounce effect 2d 1 Answer