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 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 ();
line = GetComponent ();
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().connectedBody = joints[j+1].rigidbody;
}
joints [0].AddComponent().connectedBody = thomas.rigidbody;
joints [vertexCount - 1].GetComponent ().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:
![alt text][1]
![alt text][2]
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 ().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 ().connectedBody = joints [j + 1].rigidbody;
}
joints [vertexCount - 1].GetComponent ().connectedBody = neil.rigidbody;
} else if (vertexCount < oldVertexCount) {
//int vertexDifference = oldVertexCount - vertexCount;
joints[oldVertexCount-1].GetComponent().connectedBody = null;
for(int k = oldVertexCount-1; k > vertexCount; k--){
Destroy(joints[k]);
joints.RemoveAt(k);
}
joints[vertexCount-1].GetComponent().connectedBody = neil.rigidbody;
}
}
[1]: /storage/temp/38421-screenshot01.jpg
[2]: /storage/temp/38422-screenshot02.jpg
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
↧