- Home /
Instantiating a string of prefab lights along a line renderer?
I've got a laser gun in a game that uses a line renderer to fire a long laser beam. Pretty straightforward, but it's a very central part of what the player does, so I'd like to make it look good. I'd like to include a script that instantiates a basic prefab point light I've made at multiple points along the line, so that the laser illuminates the walls nearby.
Before the naysayers jump on me, I should clarify:
a) I realize the illumination won't be perfectly even unless I use a ridiculous number of lights, and I'm okay with a variance in the brightness because of it.
b) I'm not making them shadow-casting lights.
c) I'm sure there may well be a better way to pull off the effect, and if you have one, by all means.
One last thing I'd like to note, both the laser and the targets move around, as you might expect, so the length of the laser changes. If I use a set number of lights, that will make the glow respond unexpectedly during gameplay, so I had considered using something like Vector3.Distance to determine how far away the target is, and therefore, how many evenly-spaced lights to use.
For reference's sake, I included the script I have now that runs the line renderer laser effect, but I'm not asking for more than a push in the right direction or suggestion on how I might do it... I've been writing scripts all week and my mind is a little fried, haha. Thanks in advance!
var lineRenderer : LineRenderer;
var laser : GameObject;
var target : GameObject;
var laserMaterial : Material;
var firing : boolean = false; // Is laser firing?
var laserGlowLight : Light; // Light prefab for glow effect
private var i : int = 0;
function Start () {
}
function Update () {
if (i == 0) // Initial instance of line renderer at game start. For some reason, function Start () wasn't working.
{
lineRenderer = gameObject.AddComponent(LineRenderer);
lineRenderer.material = laserMaterial;
lineRenderer.castShadows = false;
lineRenderer.receiveShadows = false;
lineRenderer.SetWidth(0.2,0.2);
lineRenderer.SetVertexCount(2);
i = 1;
}
if (firing) // Enables laser if firing.
{
lineRenderer.enabled = true;
lineRenderer.SetPosition(0,laser.transform.position);
lineRenderer.SetPosition(1,target.transform.position);
}
if (firing == false) // Disables line renderer.
{
lineRenderer.enabled = false;
}
}
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Line Renderers tied to ball keep teleporting/glitching after instantiate 1 Answer
Associate objects to a prefab 1 Answer
Editor Script to place prefabs with some variance 0 Answers
instantiating vertically 2 Answers