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
0
Question by sk00ter · Feb 11, 2015 at 08:45 PM · vector3linerendererreflectionmirrorlaser

Laser Render Reflect Angle incorrect

I'm shooting a linerender beam out of the foward position of a gameobject and if it hits any object with the tag "Mirror" I want it to bounce off at the correct angle. However, there's something wrong with my laser reflect code, and I'm inexperienced in this aspect of Unity. For some reason, when the laser hits an object with a 45 degree angle on it, it seems to pass through the mirror object and shoot off at a larger angle. View my code below:

 void RenderLaser(){
         laserLine.SetColors (color, color);
         laserLine.SetPosition (0, transform.position);
         laserRay.origin = transform.position;
         laserRay.direction = transform.forward;
 
         if (Physics.Raycast (laserRay, out laserHit, range)) {
             laserLine.SetPosition (hitCount, laserHit.point);
             if(laserHit.collider.tag == "Mirror"){
 
                 hitCount += 1;
                 laserLine.SetVertexCount(hitCount+2);
 
                 laserLine.SetPosition (hitCount, laserHit.point);
 
                 Debug.Log(transform.position);
                 Debug.Log(laserRay.origin);
                 Debug.Log(laserHit.normal);
                 Debug.Log(laserHit.point);
 
                 Vector3 reflectPos = Vector3.Reflect(transform.position, laserHit.normal);
 
                 hitCount += 1;
                 laserLine.SetPosition(hitCount, reflectPos);
             }
 
         } else {
             laserLine.SetPosition (hitCount, laserRay.origin + laserRay.direction * range);
             return;
         }
     }


Thanks in advance!

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
1
Best Answer

Answer by hexagonius · Feb 11, 2015 at 08:53 PM

The calculation of the reflectPos is not correct. Vector3.Reflect returns a direction, not a position.

try this

 Vector3 reflectPos = Vector3.Reflect(laserRay.direction, laserHit.normal);
 laserLine.SetPosition(hitCount, laserHit.point + reflectPos);


I'm to lazy to check if reflectPos is normalized and of the length you want though :)

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 sk00ter · Feb 12, 2015 at 01:53 AM 0
Share

hey nice! The reflection went at the correct angle but for some reason the initial laserLine end-position width is zero and the "bounced" laserLine width remains a constant .075 (I set this on the component) but only travels 1 unit from the "bounce" origin. Trying to debug with this code:

 Vector3 reflectPos = Vector3.Reflect(laserRay.direction, laserHit.normal);
                 Debug.Log (reflectPos);
                 Debug.Log (laserHit.point);
                 hitCount += 1;
                 laserLine.SetPosition(hitCount, laserHit.point + reflectPos);
                 Debug.Log (laserHit.point + reflectPos);

In this case:

reflectPos = (-1,0,0)

laserHit.point = (-3,1,0)

laserHit.point + reflectPos = (-4,1,0)

I understand what is happening, but I'm not sure how to solve it. It's setting the endpoint of the bounce to a defined Vector3 (laserHit.point + reflectPos) ins$$anonymous$$d of the next gameobject collider.

We're close. Any ideas?

avatar image sk00ter · Feb 12, 2015 at 03:22 AM 0
Share

Never$$anonymous$$d, just had to multiply it by my range and it went on until it reached that point. Ended up with:

 if (Physics.Raycast (laserRay, out laserHit, range)) {
             Debug.Log(laserHit.collider.tag);
             if(laserHit.collider.tag == "$$anonymous$$irror"){
                 hitCount+=1;
                 laserLine.SetVertexCount(hitCount+2);
                 //$$anonymous$$irror Hit : hit 1
                 laserLine.SetPosition (hitCount, laserHit.point);
 
                 //Reflect
                 Vector3 reflectPos = Vector3.Reflect(laserRay.direction, laserHit.normal);
 
                 //Hit nothing from mirror
                 hitCount+=1;
                 laserLine.SetPosition(hitCount, laserHit.point + reflectPos * range);
 
             }else if(laserHit.collider.tag != "$$anonymous$$irror"){
                 laserLine.SetVertexCount(hitCount+2);
                 laserLine.SetPosition (hitCount+1, laserHit.point);
                 return;
             }
 
         } else {
             hitCount+=1;
             laserLine.SetPosition (hitCount, laserRay.origin + laserRay.direction * range);
             return;
         }


For some reason the initial laserLine end-position width is still zero, though :/

avatar image hexagonius · Feb 12, 2015 at 07:08 AM 0
Share

Have you tried to initially call LineRenderer.SetWidth(). Dunno the defaults, but maybe it starts off 0.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

LineRenderer End Width Bug 2 Answers

Create a 2D line reflection (laser trace) 1 Answer

Basic LineRender Reflection 2 Answers

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


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