Update: I don’t have any of the code anymore, I’m sorry, but this thread is all that’s left. I can’t PM you a working example anymore
Hi
Some time ago I was came across the “Rope Script” made by a user called Jake. However, his rope was all fine for 3D games, but way too unpredictable for 2D games. So I set out to make my own rope.
Instead of explaining the details and how amazing I am, just take a look:
Get your hands on a working Lerpz With Rope example [DEAD LINK]
So, how to make this? I will explain it step by step.
1. the rope
The rope consists of several segments connected through character joints. Only the topmost segment has a hinge joint instead.
So, how to build the rope? Unfortunately, I have no idea, how to automate this process, so you’ll have to do it by hand. If anyone can figure out how to make a script that does the work, be my guest.
Start off with one segment (e.g. a capsule).
Give it the following componnts: a collider (capsule recommended), rigidbody and character joint.
Set the joint’s properties for Axis to (1, 0, 0) and Swingg Axis to (0, 0, 1). I am not sure about this, but it works for me.
Do the same for the next segment, which is above the one before.
Go back to the fist segment and in the Character Joint component set “Connected Body” to the second segment.
Here is a screenshot for reference:
Do the same thing for all the other segments accordingly, always connecting it to the segment right below it.
When you are about to make the final, topmost segment, give it a hinge joint.
Set Axis to (1, 0, 0) as well, but leave “Connected Body” empty. This will connect the joint to the world, keeping it in place, and providing a reliable holder.
As a final note, I set the layer of all segments, including the final one, to a layer, that can only collide with the player. So the rope will not be interrupted by enemies, powerups, or even other ropes. Usualy this is desirable in a game, as you don’t want the rope to become unreachable thus making the player stuck.
2. restricting the rope to 2D
Even though the player is seeing the game as a 2D game, everything still works in 3D. So the rope might move along the z-Axis into the back- or foreground, an the player character will seem to walk through it.
To restrict the rope i used a simple script. Attach it to every segment. It might be bad for performance, so you might want to be able to deactivate the rope when the player is too far away. Figure out what suits your needs best.
Anyway, here is the script:
//prevents the object from moving and rotating in a way unfit for 2D
function FixedUpdate () {
transform.localPosition.z = 0;
transform.eulerAngles.x = 0;
transform.eulerAngles.y = 0;
}