- Home /
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
Answer by clunk47 · Dec 17, 2012 at 03:39 AM
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;
}
}
Here is something to get you started. Study it, tweak it, experiment. Don't attach to the main camera or you won't see the line. Attach this to a child of the main camera like a gun or just attach to the First Person Controller. When you aim and click on objects, it will draw a line from the object the script is attached to, to the object point your mouse cursor ray is hitting. You will need to add a material to the line Renderer component, this script will automatically add a Line Renderer Component to the Object you attach it to. Create a new C# script and name it RayGun, because the name of the script must be the same as the class (notice I have public class Raygun in the script). Hope this gets you started :)
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(LineRenderer))]
public class RayGun : $$anonymous$$onoBehaviour
{
Vector2 mouse;
RaycastHit hit;
float range = 100.0f;
LineRenderer line;
public $$anonymous$$aterial line$$anonymous$$aterial;
void Start()
{
line = GetComponent<LineRenderer>();
line.SetVertexCount(2);
line.renderer.material = line$$anonymous$$aterial;
line.SetWidth(0.1f, 0.25f);
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, range))
{
if(Input.Get$$anonymous$$ouseButton(0))
{
line.enabled = true;
line.SetPosition(0, transform.position);
line.SetPosition(1, hit.point + hit.normal);
}
else
line.enabled = false;
}
}
}
I am using UnityScript, but I will see what I can do with this. Will update with results and any further inquiries. Thanks! :)
I'll convert it to UnityScript, but it might be an hour or more because I have some other work :)
Worked like a charm! I had to tweak it a bit to suit my needs but it works very well so thank you very much, sir! Lovely code. You made it very modular for me and extremely understandable so once again, thank you for your time!
Answer by Daniil-Besedin · Jan 01, 2016 at 03:02 PM
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).
Answer by CatsPawGames · Jan 05, 2015 at 12:26 PM
You can try ArcReactor -it will take care of raycasting, shaping and visualizing beam.
Nice package (that's what she said), but I think the op is looking for a way to understand how something like this works, rather than paying $25 for something to do it for him. I understand this may be open source, but even if it is, you should really advertise your add-on in the forum, and focus more towards actually explaining how things work in UA.
I understand that and the fact that satisfying answer already been given, I posted it for other users who search Answers trying to solve similiar problems - it can be more convinient for someone to use ready package.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085525im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I make a gui with buttons pop up in javasript? 1 Answer
Collision with Player and DoorTriggerLeft 0 Answers
Gun Firing help? 2 Answers
Full Screen Web Player? 7 Answers
Printing Distance b/w two points using line renderer and Raycast 1 Answer