- Home /
 
               Question by 
               vodanhT · Mar 07, 2019 at 10:23 AM · 
                updateline rendererhingejoint2d  
              
 
              Help: Create line renderer rope with hinge joint 2D make FPS go down badly
I wrote a script to create ropes with hinge joint 2D and line renderer, but when i ran it the FPS go down badly and make game very lag. Any one help me please!
Note: I made about 20 ropes with 18 links joint together
Here is code:
public class MyRope : MonoBehaviour {
 public Rigidbody2D hook;
 public GameObject linkPrefab;
 private GameObject[] link;
 CatmullRomSpline splineX;
 CatmullRomSpline splineY;
 CatmullRomSpline splineZ;
 float[] xPositions;
 float[] yPositions;
 float[] zPositions;
 public int links = 18;
 private LineRenderer renderer;
 void Start()
 {
     GenerateRope();
     renderer = GetComponent<LineRenderer>();
 }
 void GenerateRope()
 {
     link = new GameObject[links];
     Rigidbody2D previousRB = hook;
     for (int i = 0; i < links; i++)
     {
         link[i] = Instantiate(linkPrefab, transform);
         link[i].transform.SetParent(transform);
         link[0].GetComponent<HingeJoint2D>().connectedAnchor = new Vector2(0, 0);
         //link[0].GetComponent<MyHairFollow>().enabled = false;
         HingeJoint2D joint = link[i].GetComponent<HingeJoint2D>();
         joint.connectedBody = previousRB;
         if (i < links - 1)
         {
             previousRB = link[i].GetComponent<Rigidbody2D>();
         }
     }
     var renderer = GetComponent<LineRenderer>();
     renderer.positionCount = links;
     xPositions = new float[links];
     yPositions = new float[links];
     zPositions = new float[links];
     splineX = new CatmullRomSpline(xPositions);
     splineY = new CatmullRomSpline(yPositions);
     splineZ = new CatmullRomSpline(zPositions);
 }
 public void Update()
 {
     for (int i = 0; i < links; i++)
     {
         link[i] = this.transform.GetChild(i + 1).gameObject;
         if (link[i] == null && renderer == null)
         {
             Debug.Log("link at " + i + " NULL");
             Debug.Log("renderer" + " NULL");
         }
         else
         {
             renderer.SetPosition(i, link[i].transform.position);
             Vector3 position = link[i].transform.position;
             xPositions[i] = position.x;
             yPositions[i] = position.y;
             zPositions[i] = position.z;
             renderer.SetPosition(i, new Vector3(
             splineX.GetValue(i),
             splineY.GetValue(i),
             splineZ.GetValue(i)));
         }
     }
 }
}
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                