C# 3D Collider Without Fixed Points or use Mesh Filter Instead?
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;
}
}
}
What exactly is the question here, do you want your lightning to have some sort of collider so it can interact with objects as it strikes? Use raycasting for the parts of the lightning, would be my advice. Don't start to mess around with mesh colliders that cost you more performance than you have..
3d Colliders are primitive shapes like box, sphere, capsule, wheel, etc..but they can be manipulated to some degree. They aren't fixed to their starting shape.
You could naturally use a thin capsule collider than spans across two points of the line, as it can be resized in radius and height.
I suggest you read up on 3D colliders:
https://unity3d.com/learn/tutorials/topics/physics/colliders
http://docs.unity3d.com/$$anonymous$$anual/CollidersOverview.html
etc..
Your answer
Follow this Question
Related Questions
OnTriggerEnter Issue - Collider problem 0 Answers
Hiding part of mesh 0 Answers
Can someone please help me with mesh extrusion 0 Answers
How do i use lines to calculate triangles on mesh?? 0 Answers
How do i calculate mesh on a spline?? 0 Answers