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 anuraggoes3d · Dec 05, 2016 at 07:30 AM · reflectionlaser beam

Reflecting lasers on a mirror with a collider?

I am a beginner in Unity. So I have implemented the following script with the help of some tutorials to create a laser using line renderer and raycasting. The laser comes in fine but the reflection is confusing and is happening but the direction that it takes I cant understand. If someone could help it shall be great!!

I have the following questions-

  1. Does this code look correct for my required application?

  2. The reflections I am getting are pretty random and are not along the surface normals. Could there be something during modelling that could be causing this incorrect surface reflections?

  3. Using LaserLine.SetPosition (NumOfVert, pos); after the second vector onwards reduces the width of my laser, what could be the reason for this? But if i dont use that the laser still reflects but i dont know in what direction and based on what criteria, BUT its width remains

               using UnityEngine;
         using System;
         using System.Collections;
    
         public class LaserTip_LaserScript : MonoBehaviour 
         {
             int NumOfVert;
             LineRenderer LaserLine;
             Light FlareLight;
             Ray LaserRay;
             float LaserWidth= 0.05f;
    
         
    
             void Start () 
             {
                 LaserLine = gameObject.GetComponent<LineRenderer> ();
                 LaserLine.enabled = false;
    
                 FlareLight= gameObject.GetComponent<Light> ();
                 FlareLight.enabled = false;
    
             }
             
             // Update is called once per frame
             void Update () 
             {
    
                 if( Input.GetButtonDown("Fire1") )
                 {
                     LaserLine.SetWidth (LaserWidth, LaserWidth);
                     NumOfVert = 1;
                     LaserRay=    new Ray( transform.position, transform.forward);
    
                     StopCoroutine("FireLaser");
                     StartCoroutine("FireLaser");
                 }
             }
    
             IEnumerator FireLaser()
             {
                 LaserLine.enabled=         true;
                 FlareLight.enabled =     true;
    
                 LaserLine.SetPosition(0, LaserRay.origin);
    
                 while( Input.GetButton("Fire1")){
                     RaycastHit LaserRayHit;
                     if (Physics.Raycast (LaserRay, out LaserRayHit, 1000)) {
                         LaserLine.SetPosition (NumOfVert, LaserRayHit.point);
                         NumOfVert++;
                         print (NumOfVert);
                                                 while(LaserRayHit.collider.tag == "MirrorCollider")
                         {
                             //Debug.DrawRay(LaserRayHit.point, LaserRayHit.normal * 10, Color.green);
                             LaserLine.SetVertexCount (NumOfVert);
                             NumOfVert++;
                             Vector3 pos = Vector3.Reflect (LaserRay.direction, LaserRayHit.normal);
                             LaserLine.SetPosition (NumOfVert, pos);
                             LaserRay= new Ray( LaserRayHit.point, LaserRayHit.normal);
                             Physics.Raycast (LaserRay, out LaserRayHit, 1000);
                         }
    
                         //}
                     }
                     NumOfVert = 1;
                     yield return null;
                 }
                                 LaserLine.enabled= false;
                 FlareLight.enabled = false;
             }
    
             }
    
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 exp626stitch · Dec 22, 2020 at 03:31 PM

@anuraggoes3d 3 things:

  1. Can you post a picture of the result

  2. You are using 2 obselete functions, try fixing those.

  3. Are the objects that you are reflecting off of unity objects, or are they imported objects. If they are imported, try with a built in unity object, and if it works, its a problem with the normals of your imported object, align its Vectors with the directions in unity, that might fix it.

Comment
Add comment · Show 1 · 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 exp626stitch · Dec 22, 2020 at 03:51 PM 0
Share

Just tested your script, I have no idea why it is doing that, it could be the obsolete functions:

  1. LaserLine.SetWidth(LaserWidth, LaserWidth);

  2. LaserLine.SetVertexCount(NumOfVert);

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help with reflecting in 2D. 1 Answer

Creating a mirror effect 1 Answer

transparent + reflection shader (river water problems) 2 Answers

Iphone Shader Reflection Map 0 Answers

How to reflect an object. 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