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 25, 2015 at 06:13 AM · linerendererreflectionmirrorlaserwidth

LineRenderer End Width Bug

I have a script that renders a laser from the front of a game object and 'bounces' off of gameobjects with the tag "mirror". You can see that in the image below:

alt text

However something weird happens to the ending width of the laser after the first bounce. You can see in the image below, the 'farthest' out laser i.e. the last segment (marked as "4") bounced ending width is fine. It is .075 in the beginning and .075 at the end. The other segments however taper down to 0 from .075, and it changes.

For example if I spawn a block in the lasers path on a different segment, that segment's end width returns to .075. Image below:

alt text

The segment marked "2" ending width now remains constant where segment "1" tapers. It seems the last segment to bounce retains it's ending width, but the previously bounced segments do not.

Any Ideas? Code Below:

 void RenderLaser(){
          hitCount = 1;
          laserActive = true;
          teleActive = false;
          curPos = transform.position;
          curRot = transform.forward;
          laserLine.SetVertexCount(1);
          laserLine.SetPosition (0, transform.position);
          
          while (laserActive) {
              hitCount++;
              laserLine.SetVertexCount(hitCount);
              if (Physics.Raycast (curPos, curRot, out laserHit, range)) {
                  
                  //hitCount++
                  curPos = laserHit.point;
                  curRot = Vector3.Reflect(curRot, laserHit.normal);
                  laserLine.SetPosition(hitCount-1, laserHit.point);
                  if(laserHit.collider.tag != "Mirror"){
                      laserActive = false;
                      if(laserHit.collider.tag == "WinFace"){
                          Debug.Log("Teleport Activated.");
                          teleActive = true;
                      }
                  }
              } else {
                  //hitCount++
                  laserActive = false;
                  laserLine.SetPosition(hitCount-1, curPos+100*curRot);
              }
              if (hitCount > limit){
                  laserActive = false;
              }
              //END WHILE
          }
          
      }


screen-01-marked.png (39.3 kB)
screen-02-marked.png (39.3 kB)
Comment
Add comment · Show 2
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 siaran · Feb 25, 2015 at 12:32 PM 0
Share

does this also happen if you just use your linerenderer to draw a line where you just set the locations manually and nothing happens to it code-wise?

avatar image sk00ter · Feb 25, 2015 at 02:37 PM 0
Share

No if I draw the points manually the laser widths are fine and remain constant, but drawing them manually defeats the purpose behind the mechanics I intend for this game. :/

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Lewwwer · Jul 16, 2016 at 01:47 PM

Hi all! I know it's an old post but I met with this problem in my game and based on @NoseKills 's solution I made a function adding some extra points to a Vector3 array in order to display lines correctly.

 Vector3[] solution(Vector3[] original)
     {
         Vector3[] res = new Vector3[original.Length * 3 - 2];
         for (int i = 0; i < res.Length; i++)
         {
             if (i % 3 == 0)
             {
                 res[i] = original[i / 3];
             }
             else if (i % 3 == 1)
             {
                 res[i] = Vector3.Lerp(original[(i - 1) / 3], original[(i + 2) / 3], 0.0001f);
             }
             else if (i % 3 == 2)
             {
                 res[i] = Vector3.Lerp(original[(i + 1) / 3], original[(i - 2) / 3], 0.0001f);
             }
         }
         return res;
     }


Basically it puts extra points before and after every positions, so the lines between them will be rendered correctly.

In my game I wanted to make a star of lines centered at the gameObject and pointing to each element of an array of GameObjects. I will post it here to show how to use this function:

 using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(LineRenderer))]
 public class StarLine : MonoBehaviour {
 
     public GameObject[] targets;
     private LineRenderer lr;
     private Vector3 pos;
 
     void Start ()
     {
         lr = GetComponent<LineRenderer>();
     }
     
     
     void Update () 
     {
         pos = transform.position;
 
         Vector3[] points = new Vector3[targets.Length*2];
         for (int i = 0; i < points.Length; i++)
         {
             if (i % 2 == 0)
             {
                 points[i] = targets[i/2].transform.position;
             }
             else
             {
                 points[i] = pos;
             }
         }
 
         Vector3[] good = solution(points);
 
         lr.SetVertexCount(good.Length);
         lr.SetPositions(good);
     }
 
     Vector3[] solution(Vector3[] original)
     {
         Vector3[] res = new Vector3[original.Length * 3 - 2];
         for (int i = 0; i < res.Length; i++)
         {
             if (i % 3 == 0)
             {
                 res[i] = original[i / 3];
             }
             else if (i % 3 == 1)
             {
                 res[i] = Vector3.Lerp(original[(i - 1) / 3], original[(i + 2) / 3], 0.0001f);
             }
             else if (i % 3 == 2)
             {
                 res[i] = Vector3.Lerp(original[(i + 1) / 3], original[(i - 2) / 3], 0.0001f);
             }
         }
         return res;
     }
 
 
 }
 

I hope it helps solving this problem in the future!

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 HZ51 · Jul 02, 2018 at 04:37 AM 0
Share

Thanks a lot for that code. You saved my life :)

avatar image
0

Answer by NoseKills · Feb 25, 2015 at 02:13 PM

The linerenderer draws the line pretty much with quads and because it needs to make the flat end of each segment match with the start of next segment, it needs to tilt the quads/triangles that make the line, thus the segments are not always facing camera.

I've been able to get satisfying results by adding 2 nodes instead of one to each spot where the line changes direction. Not sure if that always works.

P.S. nice looking game :)

Comment
Add comment · Show 2 · 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 25, 2015 at 02:41 PM 0
Share

Interesting. So every time the laser "bounces" you add a very small new segment by adding a new node very close to the raycast hit.point? And thank you :)

avatar image NoseKills · Feb 25, 2015 at 06:11 PM 0
Share

Yeah that's it. I can't remember my findings exactly anymore, but maybe even just adding 2 nodes at the same spot might do it.

I was doing this for a 2D match 3 game a la Jelly Splash so my case was a bit simpler, but I'd believe the same solution should work

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

6 People are following this question.

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

Related Questions

Laser Render Reflect Angle incorrect 1 Answer

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

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

Laser LineRenderer Tapers after first Reflect 0 Answers

How to make a mirror in 3.x? 0 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