Ray Gun / Laser Beam

Hi, all!

I’d like to create a Ray Gun using the Line Renderer, but I have no experience with that particular component. I also understand that this would make heavy us of RayCasting, which I don’t quite understand. I am trying to have a ray-line be emanated from a specific point, and have it be projected towards the direction the cursor is pointing, in other words lining it up with the reticle, and having it stop rendering at the first object it hits. Any ideas?

Thanks!- YA

Ok for users of UnityScript, I have converted my original answer from C# to UnityScript. I am keeping both versions up so users of both C# and UScript may find this helpful. Here is RayGun.JS

#pragma strict

@script RequireComponent (LineRenderer)


    var mouse : Vector2;
    var hit : RaycastHit;
    var range : float = 100.0;
    var line : LineRenderer;
    var lineMaterial : Material;
    var ray : Ray;

    function Start()
    {
       line = GetComponent(LineRenderer);
       line.SetVertexCount(2);
       line.renderer.material = lineMaterial;
       line.SetWidth(0.1f, 0.25f);
    }

    function Update()
    {
       ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       if(Physics.Raycast(ray, hit, range))
       {
         if(Input.GetMouseButton(0))
         {
          line.enabled = true;
          line.SetPosition(0, transform.position);
          line.SetPosition(1, hit.point + hit.normal);
         }
         else
          line.enabled = false;
       }

    }

This is how I solved that problem:

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(LineRenderer))]
public class LaserScript : MonoBehaviour
{
	public float range = 1000;
	private LineRenderer line;
	public bool playerOnly = true;

	void Start ()
	{
		line = GetComponent<LineRenderer> ();
		line.SetVertexCount (2);
	}

	void Update () // consider void FixedUpdate()
	{
		RaycastHit2D hit = Physics2D.Raycast (transform.position, transform.right, range); // transform.position + (transform.right * (float)offset) can be used for casting not from center.
		if (hit) {
			line.SetPosition (0, transform.position);
			line.SetPosition (1, hit.point);
			Collider2D collider = hit.collider;
			if (collider.gameObject.tag == "Player") {
				// Register hit.
			}
		} else {
			line.SetPosition (0, transform.position);
			line.SetPosition (1, transform.position + (transform.right * range)); // (transform.right * ((float)offset + range)) can be used for casting not from center.
		}
	}
}

I need a constant beam from one point in direction that I could easily modify through transform. Also it has range setting. All other values should be set in Line Randerer itself.

If you want to use it in 3D you should be fine if you just remove “2D” from class names. (All methods are the same.)

For beginners: “transform.right” can be replaced with 2 other directions (transform.up, transport.forward); custom direction is set using vector (2 in 2D, 3 in 3D).

You can try ArcReactor -it will take care of raycasting, shaping and visualizing beam.