- Home /
Pulling up a Rope
Hi Guys!
I made a physical rope, using empty gameobjects, hingeJoints and line renderer - and it is working remarkably well. However, I would like to be able to pull in the rope (like rolling up around your arm, and pulling up objects) and that.. I cannot do :(
I uses this code to generate the rope between two characters: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class RopeGeneratorScript : MonoBehaviour {
private LineRenderer line;
private List<GameObject> joints;
private int vertexCount;
private float NTDistance;
public GameObject emptyPrefab;
public GameObject neil;
public GameObject thomas;
// Use this for initialization
void Start () {
vertexCount = (((int)Vector3.Distance (neil.transform.position, thomas.transform.position))*4)-1;
joints = new List<GameObject> ();
line = GetComponent<LineRenderer> ();
line.SetWidth (0.05f, 0.05f);
line.SetColors (Color.black, Color.blue);
for (int i = 0; i < vertexCount; i++) {
joints.Add((GameObject)Instantiate(emptyPrefab, new Vector3(transform.position.x, transform.position.y, transform.position.z + (i+1)*0.25f), Quaternion.identity));
}
for(int j = 0; j < joints.Count-1; j++){
joints[j].transform.parent = this.transform;
joints[j].GetComponent<HingeJoint>().connectedBody = joints[j+1].rigidbody;
}
joints [0].AddComponent<HingeJoint>().connectedBody = thomas.rigidbody;
joints [vertexCount - 1].GetComponent<HingeJoint> ().connectedBody = neil.rigidbody;
}
// Update is called once per frame
void Update () {
line.SetVertexCount (joints.Count);
for(int i = 0; i < joints.Count; i++){
line.SetPosition(i, joints[i].transform.position);
}
}
}
As I said it works quite well - here is a screen of how it looks like:
Now picture one shows how it looks when its started - BUT in picture #2 I would like to be able to pull the lower character up, using the rope.. I tried extending and shorting the length of the rope, but it just didn't work very well at all - here is my attempt:
void UpdateRobe(){
int oldVertexCount = vertexCount;
vertexCount = (((int)Vector3.Distance (neil.transform.position, thomas.transform.position))*4)-1;
if (vertexCount == oldVertexCount) {
return;
} else if (vertexCount > oldVertexCount) {
joints [oldVertexCount - 1].GetComponent<HingeJoint> ().connectedBody = null;
int vertexDifference = vertexCount - oldVertexCount;
for (int i = 0; i < vertexDifference; i++) {
joints.Add ((GameObject)Instantiate (emptyPrefab, new Vector3 (joints [oldVertexCount - 1].transform.position.x, joints [oldVertexCount - 1].transform.position.y, joints [oldVertexCount - 1].transform.position.z + (i + 1) * 0.25f), Quaternion.identity));
}
for (int j = oldVertexCount-1; j < vertexCount-1; j++) {
joints [j].transform.parent = this.transform;
joints [j].GetComponent<HingeJoint> ().connectedBody = joints [j + 1].rigidbody;
}
joints [vertexCount - 1].GetComponent<HingeJoint> ().connectedBody = neil.rigidbody;
} else if (vertexCount < oldVertexCount) {
//int vertexDifference = oldVertexCount - vertexCount;
joints[oldVertexCount-1].GetComponent<HingeJoint>().connectedBody = null;
for(int k = oldVertexCount-1; k > vertexCount; k--){
Destroy(joints[k]);
joints.RemoveAt(k);
}
joints[vertexCount-1].GetComponent<HingeJoint>().connectedBody = neil.rigidbody;
}
}
So I was thinking if instead of making the rope longer and shorter than I could just wind it up and out, but I have no idea where to start really :O this is my first attempt with ropes so I on shaky ground as it is :)
Best Regards Olle
Answer by RetepTrun · Jan 09, 2015 at 03:01 AM
Nice. The rope looks fun as is.
My $0.2:
Tried shortening the rope by scaling down all its links instead of deleting them?
Adding force to the guy being hauled up as you delete the links?
A different but less nice looking approach to ropes? http://gamedevelopment.tutsplus.com/tutorials/swinging-physics-for-player-movement-as-seen-in-spider-man-2-and-energy-hook--gamedev-8782
Thanks for the reply
Not sure what you mean? unless you are thinking of the individual lings as cylinders or cubes. Each link consist simply of an empty gameObject and a sphere collider. So each hingeJoint is connected to a mathematical point in space - and as far as I can tell there is no way to scale the length of the joint after you run the game? Have tried to move around the anchors at runtime, but it doens't seem to make much difference :(
Have not tried that, may be a good idea to try it ;) However that still leaves me with the problem of extending the rope length again, which worked really poorly for me as well :(
good luck then. $$anonymous$$y last idea is just provide the characters with a cylinder and a crank and turn it around like this thing for buy I saw awhile ago.
@vexe sorry I'll remember that in the future :)
@RetepTrun I had the same thought, and I might have to try it out - though first I'm trying to add negative force to individual joints, but I will definiately try out the cylinder/crank option as well.
I just tried making a crank, and it works fantastic! It needs A LOT of polishing - but the initial draft works wonders.
For other devs. who are attempting this I simply made a cylinder, and attached to additional cylinders on either side (slimmer and wider) - Each cylinder has a capsule collider, and the large middel cylinder has a rigidbody. The end of the rope is attached to the cylinder, and then I simply rotate the object around its own Y-axes. You need enough angle per second to add enough force to pull the rope and the object in the end (for me about 500) but it depends on the weight of the object. Here is a screenshot:
hope it helps some people out there!
@Ollenator Hi! I'm trying to achieve something similar and I decided to use your script as a start point (looked really nice and easy). However, when I try to do it, as soon as press play, my rope goes crazy from the beginning.
How must be the hingeJoint configured in the Prefab? I'm starting to learn how to tune joints, but I don't think I fully understand them yet...
Here a GIF:
https://dl.dropboxusercontent.com/u/1209521/sept-20-2016%2014-40-13.gif
Answer by Ollenator · Sep 21, 2016 at 11:19 AM
That do look rather crazy :) But to be honest, I don't remember the settings directly. It has been a long time since I used the project.
I do recall something about my rope doing the same thing as yours, but to be honest, I can't remember what I did to fix it :(
Your answer
Follow this Question
Related Questions
2D rope (hinge and distance) on only ONE axis 0 Answers
get a rigidbody rope move with my character 1 Answer
Translating a gameObject with a Hinge joint? 0 Answers
2D hinge joint with sprite 0 Answers
jungle swing type game ...? 0 Answers