- Home /
C# Assigning EdgeCollider2D's Points
I'm trying to create randomized lightning script. I was able to assign the LineRenderer's Positions But when I tried assigning EdgeCollider2D's Points it only bobs back and forth along with LineRenderer. I checked the actual component and it is using the same parameters as LineRenderer. Is there something I'm missing?
using UnityEngine;
using System.Collections;
using Generic = System.Collections.Generic;
public class beamScript : MonoBehaviour {
public Transform startPos;
public Transform beam;
public float destFloat = 15f;
public float timeToUpdate = 0.05f;
public float timeToPowerUp = 0.5f;
public float keyVertexDist = 2.0f;
int numVertices;
float lastUpdate;
// Use this for initialization
void Start () {
numVertices = 0;
lastUpdate = 0.0f;
}
// Update is called once per frame
void Update () {
if(beam.position.z < destFloat){
beam.Translate(Vector3.forward * 5 * Time.deltaTime * (1.0f / timeToPowerUp));
}
if ((Time.time - lastUpdate) < timeToUpdate)
return;
lastUpdate = Time.time;
numVertices = Mathf.RoundToInt(Vector3.Distance(startPos.position, beam.position) / keyVertexDist);
Vector3 currentV = startPos.localPosition;
Vector3[] keyVertices = new Vector3[numVertices];
GetComponent<LineRenderer>().SetVertexCount(numVertices);
Vector2[] points = new Vector2[]{};
for (int i = 0; i < numVertices; i++) {
float vfl = (Random.value * 4.0f - 2.0f);
Vector3 v = new Vector3(
currentV.x + vfl,
currentV.y + vfl,
currentV.z + vfl
);
GetComponent<LineRenderer>().SetPosition(i, v);
keyVertices[i] = v;
points = ConvertArray(keyVertices);
currentV += (beam.localPosition - startPos.localPosition) / numVertices;
}
points[0] = startPos.localPosition;
points[numVertices-1] = beam.localPosition;
GetComponent<EdgeCollider2D>().points = points;
GetComponent<LineRenderer>().SetPosition(0, startPos.localPosition);
GetComponent<LineRenderer>().SetPosition(numVertices-1, beam.localPosition);
}
Vector2[] ConvertArray(Vector3[] v3){
Vector2 [] v2 = new Vector2[v3.Length];
for(int i = 0; i < v3.Length; i++){
Vector3 tempV3 = v3[i];
v2[i] = new Vector2(tempV3.x, tempV3.y);
}
return v2;
}
Comment
Your answer
Follow this Question
Related Questions
C# Invalid Points Assigned to 2D Edge Collider 1 Answer
C# EdgeCollider2D Points Not Updating Continuously 0 Answers
Multiple Cars not working 1 Answer
I can't get 2DCollider to work. 1 Answer
For Some reason, collider check has extra collider. 2 Answers