Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by IgorUnity3D · Oct 25, 2016 at 05:34 AM · vector3linerendererreflectionlasertrace

Create a 2D line reflection (laser trace)

Hello,

I'm trying to implement a line (laser) collides with some scenery objects. After my search, I saw that we can use LineRender with the Reflect function of a Vector3 (Vector3.Reflect). But still no success with my implementation (maybe I have not understood how it works).

It should look like the sample images below:

alt text

...As the dotted line blank.... ^

alt text

I have the initial code:

 var hit : RaycastHit;
 
 private var point01 = Vector3 (0,0,0);
 
 function Start () {
 
     line = this.gameObject.AddComponent (LineRenderer);
 
 }
 
 function Update () {
 
     line.SetPosition(0, point01); Physics.Raycast(transform.position,transform.forward,hit); 
     
     if(hit.collider) { 
         line.SetPosition(1,Vector3(0,0,hit.distance));
         line.SetPosition(2, Vector3.Reflect(Vector3(0, 0, hit.distance), Vector3.right));
     }   
     else
     {
         line.SetPosition(1,Vector3(0,0,5000));
     }
 }


What would be the best way to get the image results?

Any suggestions will be of great help.

Thank you in advance.

line-reflection-2.png (428.0 kB)
line-reflection.jpg (163.5 kB)
Comment
Add comment · Show 1
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 doublemax · Oct 25, 2016 at 05:38 AM 0
Share

Untested, but try this:

 if(hit.collider) { 
    line.SetPosition( 1, hit.point );
    line.SetPosition( 2, hit.point + 100.0f * Vector3.Reflect(transform.forward, hit.normal) );
 }   
 else
 {
    line.SetPosition( 1, transform.position + transform.forward * 100.0f );
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BratianuCosmin · Aug 19, 2020 at 11:48 AM

some Inspiration

 public class AimLine : MonoBehaviour
 {
     RaycastHit2D oldHit;
     RaycastHit2D hit2;
 
     LineRenderer Line;
 
     public float MAXdistance;
 
     private void Start()
     {
         Line = GetComponent<LineRenderer>();
     }
 
     private void Update()
     {
         float totaldistance = 0;
         Line.positionCount = 1;
         Line.SetPosition(0, Vector3.zero);
 
         oldHit = Physics2D.Raycast(Vector3.zero, (Vector2)transform.up, 200f);
 
         var direction = Vector2.Reflect(transform.up, oldHit.normal);
 
         for (int i = 0; i < 100; i++)
         {
             if (totaldistance < MAXdistance)
             {
                 if (oldHit.collider)
                 {
                     totaldistance += oldHit.distance;
                     Line.positionCount++;
                     Line.SetPosition(Line.positionCount - 1, oldHit.point);
                     oldHit.collider.enabled = false;
                     hit2 = Physics2D.Raycast(oldHit.point, direction, 200f);
                     direction = Vector2.Reflect(direction, hit2.normal);
                     oldHit.collider.enabled = true;
                     oldHit = hit2;
                 }
             }
             else
             {
                 break;
             }
         }
     }
 }
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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Laser Render Reflect Angle incorrect 1 Answer

Dynamically adjusting LineRenderer vertexes to follow raycasts 0 Answers

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

Only One LineRenderer Renders 0 Answers

LineRenderer End Width Bug 2 Answers


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