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*.transform.position);*
-
}*
}
}
As I said it works quite well - here is a screen of how it looks like:
[38421-screenshot01.jpg|38421]*
[38422-screenshot02.jpg|38422]*
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 this is my first attempt with ropes so I on shaky ground as it is
Best Regards
Olle