Pew Pew! Oh Wait... Laser Script Not Working! :( (Video Included)

Hi there! I’m new to Unity. That said, I tried recreating Mike Geig’s laser script.

My outcome–in this case–is for the wall/floor to totally stop the laser. As of now, the laser passes right through my terrain. Can you help me debug this script?

I’ve looked at this script for an hour and twelve minutes. Haven’t found the problem! :stuck_out_tongue: So… thanks for your help!

(Video’s below the script!)

using UnityEngine;
using System.Collections;

public class lsrScript : MonoBehaviour
LineRenderer line;

void Start () 
	line = gameObject.GetComponent<LineRenderer> ();
	line.enabled = false;
	Screen.lockCursor = true;

void Update () {
	if (Input.GetButtonDown ("Fire1")) 
		StopCoroutine ("FireLaser");

IEnumerator FireLaser()
	line.enabled = true;

		Ray ray = new Ray(transform.position, transform.forward);
		RaycastHit hit;

		line.SetPosition(0, ray.origin);

		if(Physics.Raycast(ray, out hit, 100))
			line.SetPosition(1, hit.point);
			line.SetPosition(1, ray.GetPoint(100));
		yield return null;

	line.enabled = false;


Here is the video!


I have the same problem with my CameraMovement script and what i’ve done to “repair” raycasting is this method:

// Set the max distance of your laser
float HitPoint = 100;
// Check if there was any hit before reaching desired distance
if(Physics.Raycast(ray, out hit, 100))
    HitPoint = hit.distance; // If there was hit change the distance value

// Get the the collision point and pass it into line renderer
line.SetPosition(1, ray.GetPoint(HitPoint));

Hope this will help.