• 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
0
Question by AGC · Mar 24, 2015 at 05:47 AM · raycastlinerendererreflectionraylaser

LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting

Hello Unity!

I have a problem with laser beam (LineRenderer) it's not following the ray direction. if the line renderer is hitting an object it will reflect and follow the ray but if there is not object the lineRenderer will not follow the ray.

how can i fix this issue below will be the code that i am just attaching to LineRenderer object.

and here is the short Video that shows how it's behaving with script attached to lineRenderer object

 using UnityEngine;
 using System.Collections;
 
 public class Reflection : MonoBehaviour 
 {
 
     //this game object's Transform  
     private Transform goTransform;  
     //the attached line renderer  
     private LineRenderer lineRenderer;  
     
     //a ray  
     private Ray ray;  
     //a RaycastHit variable, to gather informartion about the ray's collision  
     private RaycastHit hit;  
     
     //reflection direction  
     private Vector3 inDirection;  
     
     //the number of reflections  
     public int nReflections = 2;  
     
     //the number of points at the line renderer  
     private int nPoints;  
     
     void Awake ()  
     {  
         //get the attached Transform component  
         goTransform =GetComponent<Transform>();  
         //get the attached LineRenderer component  
         lineRenderer =GetComponent<LineRenderer>();  
     }  
     
     void Update ()  
     {  
         //clamp the number of reflections between 1 and int capacity  
         nReflections = Mathf.Clamp(nReflections,1,nReflections);  
         //cast a new ray forward, from the current attached game object position  
         ray = new Ray(goTransform.position,goTransform.forward);  
         
         //represent the ray using a line that can only be viewed at the scene tab  
         Debug.DrawRay(goTransform.position,goTransform.forward * 100, Color.magenta);  
         
         //set the number of points to be the same as the number of reflections  
         nPoints = nReflections;  
         //make the lineRenderer have nPoints  
         lineRenderer.SetVertexCount(nPoints);  
         //Set the first point of the line at the current attached game object position  
         lineRenderer.SetPosition(0,goTransform.position);  
         
         for(int i=0;i<=nReflections;i++)  
         {  
             //If the ray hasn't reflected yet  
             if(i==0)  
             {  
                 //Check if the ray has hit something  
                 if(Physics.Raycast(ray.origin,ray.direction, out hit, 1000))//cast the ray 100 units at the specified direction  
                 {  
                     //the reflection direction is the reflection of the current ray direction flipped at the hit normal  
                     inDirection = Vector3.Reflect(ray.direction,hit.normal);  
                     //cast the reflected ray, using the hit point as the origin and the reflected direction as the direction  
                     ray = new Ray(hit.point,inDirection);  
                     
                     //Draw the normal - can only be seen at the Scene tab, for debugging purposes  
                     Debug.DrawRay(hit.point, hit.normal*3, Color.blue);  
                     //represent the ray using a line that can only be viewed at the scene tab  
                     Debug.DrawRay(hit.point, inDirection*100, Color.magenta);  
                     
                     //Print the name of the object the cast ray has hit, at the console  
                     Debug.Log("Object name: " + hit.transform.name);  
                     
                     //if the number of reflections is set to 1  
                     if(nReflections==1)  
                     {  
                         //add a new vertex to the line renderer  
                         lineRenderer.SetVertexCount(++nPoints);  
                     }  
                     
                     //set the position of the next vertex at the line renderer to be the same as the hit point  
                     lineRenderer.SetPosition(i+1,hit.point);  
                 }  
             }  
             else // the ray has reflected at least once  
             {  
                 //Check if the ray has hit something  
                 if(Physics.Raycast(ray.origin,ray.direction, out hit, 1000))//cast the ray 100 units at the specified direction  
                 {  
                     //the refletion direction is the reflection of the ray's direction at the hit normal  
                     inDirection = Vector3.Reflect(ray.direction,hit.normal);  
                     //cast the reflected ray, using the hit point as the origin and the reflected direction as the direction  
                     ray = new Ray(hit.point,inDirection);  
                     
                     //Draw the normal - can only be seen at the Scene tab, for debugging purposes  
                     Debug.DrawRay(hit.point, hit.normal*3, Color.blue);  
                     //represent the ray using a line that can only be viewed at the scene tab  
                     Debug.DrawRay(hit.point, inDirection*100, Color.magenta);  
                     
                     //Print the name of the object the cast ray has hit, at the console  
                     Debug.Log("Object name: " + hit.transform.name);  
                     
                     //add a new vertex to the line renderer  
                     lineRenderer.SetVertexCount(++nPoints);  
                     //set the position of the next vertex at the line renderer to be the same as the hit point  
                     lineRenderer.SetPosition(i+1,hit.point);  
                 }  
             }  
         }  
     }  
 }

Here are some Photos as well that show the problemalt text alt text

thanks in advance for help!!!!

problem.png (47.0 kB)
wrongdirection-1.png (58.3 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Redugsi · May 04, 2015 at 10:39 AM

If it does not hit any object then you should use

 lineRenderer.SetPosition(index,ray.GetPoint(50));

to follow the ray.GetPoint() takes float distance as parameter.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Inconsistent Reflection/Ricochet of 2D Lines at Specific Angles via Raycasting and 2D Colliders 2 Answers

Laser LineRenderer Tapers after first Reflect 0 Answers

Shoot Laser 0 Answers

How to draw a line between objects and get distance 1 Answer

How can I can I cast a ray from a gameobject? 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