Hey folks, I spent a little bit of time working on this script to create a laser beam effect. You can attach a particle system to it to create some sparks at contact point. I suggest placing the script in an empty game object that is aligned with your weapon so it rotates with it. You can see the effect here: Code (csharp): //This is free to use and no attribution is required //No warranty is implied or given using UnityEngine; using System.Collections; [RequireComponent (typeof(LineRenderer))] public class LaserBeam : MonoBehaviour { public float laserWidth = 1.0f; public float noise = 1.0f; public float maxLength = 50.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); 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++){ //Set the position here to the current location and project it in the forward direction of the object it is attached to offset.x =myTransform.position.x+i*myTransform.forward.x+Random.Range(-noise,noise); offset.z =i*myTransform.forward.z+Random.Range(-noise,noise)+myTransform.position.z; position[i] = offset; position[0] = myTransform.position; 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); } }
nice that you publish it but if you want people to also use it you should always include licensing information. something posted in the internet is not automatically free to use (for those hwo care about their legal status). if you dont care what happens with this code and dont require credits you should consider the http://en.wikipedia.org/wiki/WTFPL or http://en.wikipedia.org/wiki/Public_domain. also you should consider putting it in the wiki.
i'm not a lawyer but as far as i know this is wrong. when no explicit licence is provided this is your intellectual property and everyone who uses it CAN be punished. if you do i or not does not matter but you could do it. therefore its always better to explicitely state the terms of use / licence with your product when you publish it. edit: thinking over your statement you could mean it as "terms of use". sorry for misunderstanding. language barrier sucks .
awesome! thank you, it looks great. If I ever need a laser beam (which could be soon) I will be sure to give this a try
Thanks for this script been looking for this. What did you use for particle system was it your own custom made system?
Old thread, I know.. but I noticed the example image link is broken. Here is the image, I found on the poster's new website. http://burrowgames.com/wp-content/uploads/2012/02/LaserBeamPewPew.png cheers
Just because licenses was cited in this thread. Don't ever make a code public domain. Why? Because "public domain" definition can change from country to country, sometimes leading to unexpected legal issues. You can use something like BSD that basically says "you can use this as long as you don't sue me (for this work) afterwards".
can you please put a download link to this script? coz on my mac mlnodevelop doesn't work! so i must use SublimeText2... and i can't seem to put the script in unity... plz put download!!!!!!
I just wanted to update people on this. Work that is published in public forms is consider "open source" and is free to be used and has no legal standing of "being sued" as its considered to be publicly distributed and accessible. Thus, if you post on something like this, discord, by default they have no grounds to sue when using it.