I have a script that randomly draws a Line Renderer into a beam like lightning and I want to implement a 3d collider into the script. But all the 3d colliders except for the mesh collider appear to have fixed points and the LineRenderer isn’t a mesh so it won’t work with the Mesh Collider. There isn’t a 3d version of the Edge Collider either so that’s out. Do I need to convert my script to using a Mesh Filter instead of a Line Renderer or are there 3d Colliders that aren’t fixed?
using UnityEngine;
using System.Collections;
public class BeamShooter : MonoBehaviour {
// Prefabs
public Transform beamPoint;
public Transform beamPointOrigin;
public LineRenderer lineRndr;
// Timing
public float timeToUpdate = 0.05f;
public float timeToPowerUp = 0.5f;
public int maxNumVertices;
public int numVertices;
public float lastUpdate;
// Use this for initialization
void Awake()
{
lineRndr = GetComponent<LineRenderer>();
numVertices = 0;
lastUpdate = 0.0f;
}
// Update is called once per frame
void Update () {
if( Input.GetButton("Fire1") ) {
if ((Time.time - lastUpdate) < timeToUpdate)
return;
lastUpdate = Time.time;
if(numVertices < maxNumVertices){
numVertices = Mathf.RoundToInt(Vector3.Distance(transform.position, beamPoint.position));
beamPoint.Translate(Vector3.forward * 5 * Time.deltaTime * (1.0f / timeToPowerUp));
}
Vector3 currentV = transform.localPosition;
lineRndr.SetVertexCount(numVertices);
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
);
lineRndr.SetPosition(i, v);
currentV += (beamPoint.localPosition - transform.localPosition) / numVertices;
}
lineRndr.SetPosition(0, transform.localPosition);
}
if(Input.GetButtonUp("Fire1")){
numVertices = 0;
lineRndr.SetVertexCount(0);
beamPoint.transform.position = beamPointOrigin.position;
}
}
}