# C# Calculate Edge Collider for Line Renderer

I need to create an edge collider for a line renderer with the following script below attached. So I was able to calculate the line renderer’s positions and vertices but I’m not sure how to implement an edge collider. I assume I want to set the edge collider’s points to equal the line renderer’s number of positions? Also I wasn’t able to find edgeCollider2d.setPoint. Is this done differently to setPosition?

``````using UnityEngine;
using System.Collections;
using Generic = System.Collections.Generic;

public class BeamScript : MonoBehaviour {
public Transform startPos;
public Transform beam;
public float destFloat = 1f;
public float timeToUpdate = 0.05f;
public float timeToPowerUp = 0.5f;
public float keyVertexDist = 3.0f;
int numVertices;
float lastUpdate;

// Use this for initialization
void Start () {
numVertices = 0;
lastUpdate = 0.0f;
}

// Update is called once per frame
void Update () {
Vector3 movePositions = new Vector3(beam.transform.position.x + Input.GetAxis("Horizontal"),beam.transform.position.y + Input.GetAxis("Vertical"),beam.transform.position.z);
beam.transform.position = Vector3.Lerp(beam.transform.position, movePositions, Time.deltaTime);
if ((Time.time - lastUpdate) < timeToUpdate)
return;
lastUpdate = Time.time;
numVertices = Mathf.RoundToInt(Vector3.Distance(startPos.position, beam.position) / keyVertexDist) * 3 + 1;
Vector3 currentV = startPos.localPosition;
Vector3[] keyVertices = new Vector3[numVertices];
GetComponent<LineRenderer>().SetVertexCount(numVertices);
for (int i = 0; i < numVertices; i++) {
Vector3 v = new Vector3(
currentV.x + (Random.value * 4.0f - 2.0f),
currentV.y + (Random.value * 4.0f - 2.0f),
currentV.z + (Random.value * 4.0f - 2.0f)
);
GetComponent<LineRenderer>().SetPosition(i, v);
keyVertices *= v;*
``````

currentV += (beam.localPosition - startPos.localPosition) / numVertices;
}
GetComponent().SetPosition(0, startPos.localPosition);
GetComponent().SetPosition(numVertices-1, beam.localPosition);
for (int i = 0; i < numVertices; i++) {
if (i % 3 == 0)
continue;
float x = (Vector3.Distance(keyVertices[i-1], keyVertices[i+2]) / 3);
for (int j = 0; j < 2; j++) {
Vector3 v = new Vector3(
keyVertices[i-1].x + (Mathf.Sin(x-(Mathf.PI/2.0f))/2.0f+0.5f) * (keyVertices[i+2].x-keyVertices[i-1].x),
keyVertices[i-1].y + (Mathf.Sin(x-(Mathf.PI/2.0f))/2.0f+0.5f) * (keyVertices[i+2].y-keyVertices[i-1].y),
keyVertices[i-1].z + (Mathf.Sin(x-(Mathf.PI/2.0f))/2.0f+0.5f) * (keyVertices[i+2].z-keyVertices[i-1].z)
);
GetComponent().SetPosition(i+j, v);
x += x;
}
i += 2;
}
}
}

You can set the points of an edge collider by assigning a Vector2 to EdgeCollider2D.points. It looks like keyVertices pretty much has everything you need already, just need to convert to an array of Vector2s.

Remember to use transform.InverseTransformPoint to go from world coordinates to local coordinates if necessary!