- Home /
Question by
rtrahms · Nov 24, 2013 at 06:49 PM ·
linerendererlasertransform.forward
Trouble making laserbeam with LineRenderer
Hi all - Like many others, I am creating a laserbeam prefab using the LineRenderer object. I have created an empty gameobject, attached a LineRender component, and attached the following script to the object as well. The intent is to extend the lineRenderer segments in the local forward direction (transform.forward). The script works, but only when the gameobject is oriented with local forward pointed in the global z direction. The closer I get to 90 degrees from global z (either in x or y), this line reduces to a dot. I have set useWorldSpace to false in the script, but I think I am missing something fundamental to align with the local forward vector. Any ideas? Thanks in advance!
Rob
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(LineRenderer))]
public class LaserBeam : MonoBehaviour {
public float laserWidth = 0.07f;
public float noise = 0.01f;
public float maxLength = 100.0f;
public Color color = Color.red;
LineRenderer lineRenderer;
int length;
Vector3[] position;
//Cache any transforms here
Transform myTransform;
Transform endEffectTransform;
//The particle system, in this case sparks which will be created by the Laser
public ParticleSystem endEffect;
Vector3 offset;
// Use this for initialization
void Start () {
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.SetWidth(laserWidth, laserWidth);
lineRenderer.useWorldSpace = false;
myTransform = transform;
offset = new Vector3(0,0,0);
endEffect = GetComponentInChildren<ParticleSystem>();
if(endEffect)
endEffectTransform = endEffect.transform;
}
// Update is called once per frame
void Update () {
RenderLaser();
}
void RenderLaser(){
//Shoot our laserbeam forwards!
UpdateLength();
lineRenderer.SetColors(color,color);
//Move through the Array
for(int i = 0; i<length; i++){
// works in the global z direction only!!
// If I rotate the parent toward 90 deg from Z, this moves down to a dot at the local origin
offset.x = Random.Range(-noise,noise);
offset.y = Random.Range(-noise,noise);
offset.z =(i*myTransform.forward.z)+Random.Range(-noise,noise);
position[i] = offset;
lineRenderer.SetPosition(i, position[i]);
}
}
void UpdateLength(){
//Raycast from the location of the cube forwards
RaycastHit[] hit;
hit = Physics.RaycastAll(myTransform.position, myTransform.forward, maxLength);
int i = 0;
while(i < hit.Length){
//Check to make sure we aren't hitting triggers but colliders
if(!hit[i].collider.isTrigger)
{
length = (int)Mathf.Round(hit[i].distance)+2;
position = new Vector3[length];
//Move our End Effect particle system to the hit point and start playing it
if(endEffect){
endEffectTransform.position = hit[i].point;
if(!endEffect.isPlaying)
endEffect.Play();
}
lineRenderer.SetVertexCount(length);
return;
}
i++;
}
//If we're not hitting anything, don't play the particle effects
if(endEffect){
if(endEffect.isPlaying)
endEffect.Stop();
}
length = (int)maxLength;
position = new Vector3[length];
lineRenderer.SetVertexCount(length);
}
}
Comment