- Home /
Question by
atharva1840 · Feb 14, 2017 at 06:28 AM ·
laser beam
I am using line renderer to shoot lasers, my code doesnt show any errors but I cant see the laser in my game. How do i resolve it?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LaserMovement : MonoBehaviour {
// Use this for initialization
LineRenderer lr;
void Start ()
{
lr = gameObject.GetComponent<LineRenderer> ();
lr.enabled = false;
}
// Update is called once per frame
void Update ()
{
if (Input.GetButton ("Fire1"))
{
StopCoroutine ("FireLaser");
StartCoroutine ("FireLaser");
}
}
IEnumerator FireLaser ()
{
lr.enabled =true;
while (Input.GetButton ("Fire1"))
{
Ray r1 = new Ray (transform.position, transform.forward);
RaycastHit hit;
lr.SetPosition (0, r1.origin);
if (Physics.Raycast (r1, out hit, 100)) {
lr.SetPosition (1, hit.point);
if (hit.rigidbody) {
hit.rigidbody.AddForceAtPosition (transform.forward * 5, hit.point);
Debug.DrawLine(r1.origin, hit.point, Color.red);
}
}
else
{
lr.SetPosition (1, r1.GetPoint (100));
}
yield return null;
}
lr.enabled = false;
}
}
Comment