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 ![]()
//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);
}
}
}
}