Destroying lasers...

Still learning how to script some basic things. I’ve managed to throw together something to act as a laser that will affect rigidbodies, but I can’t quite seem to grasp the steps to destroy the laser, after it’s been drawn. So far, everything I try, ends up in the laser not showing at all. It’s almost as if I need to add some kind of delay or timer… Here’s what I have so far. It’s probably a stupid simple solution - those are my favorite :stuck_out_tongue:

//Private Laser vars
private var laserStartWidth = 0.25;
private var laserEndWidth = 0.2;
private var laser : LineRenderer;

//Tuneable Laser vars
var force = 10.0;
var range = 1000.0;
var startPoint : Transform;
var laserMaterial : Material;

function Start(){

laser = this.gameObject.AddComponent(LineRenderer);
laser.SetWidth(laserStartWidth, laserEndWidth);
laser.SetVertexCount(2);
laser.material = laserMaterial; 


}


function Update () {

var direction = startPoint.TransformDirection(Vector3.forward);
var hit : RaycastHit;
var point01 = startPoint.position;

	if(Input.GetButtonDown("Fire1"))
	{
	Debug.Log("Fire Pressed");

	
			if (Physics.Raycast (startPoint.position, direction, hit, range)) {
			// Apply force
				if (hit.rigidbody){
				Debug.Log("Hit RigidBody");
				hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
				
				//Debug line to make sure the laser effect is actually hitting what it's supposed to
				Debug.DrawLine (startPoint.position, hit.point, Color.white);
	
				//Final point of the laser - that draws to the hit collider
				
				point02 = hit.point;

				laser.SetPosition(0, point01);
				laser.SetPosition(1, point02); 
		
				}


			}
	
	
		}

}

If you just want a brief pause before the laser turns off, you could use a yield?

Try adding an extra function to your script :

private function laserOff(){
	yield WaitForSeconds (0.125);
	laser.enabled = false;
}

then in the code where you perform the laser hit, add the following :

laser.enabled = true;
laserOff();

Is that the effect you’re after?

Excellent - worked like a charm. I’ll keep playing around with what I’ve got here - see if I can get the laser to fade out nicely.

Thanks for the help!