Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by youngapprentice · Dec 16, 2012 at 09:14 PM · javascriptlaserbeam

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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;
        }
 
     }
 
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · Dec 16, 2012 at 09:40 PM 2
Share

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;
         }
             
     }
 }
 
avatar image youngapprentice clunk47 · Dec 17, 2012 at 03:05 AM 2
Share

I am using UnityScript, but I will see what I can do with this. Will update with results and any further inquiries. Thanks! :)

avatar image clunk47 clunk47 · Dec 17, 2012 at 03:07 AM 1
Share

I'll convert it to UnityScript, but it might be an hour or more because I have some other work :)

avatar image youngapprentice clunk47 · Dec 17, 2012 at 05:23 PM 2
Share

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!

Show more comments
avatar image
2

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by CatsPawGames · Jan 05, 2015 at 12:26 PM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · Jan 05, 2015 at 07:01 PM 1
Share

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.

avatar image CatsPawGames · Jan 05, 2015 at 07:37 PM 1
Share

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.

avatar image clunk47 · Jan 05, 2015 at 07:49 PM 0
Share

True, so in that case, thanks for sharing.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges