- Home /
 
Line Renderer Positioning Problem
I just can't figure out how this works, I've been figuring out how to make, what I want but It just won't work. Lots of time, less of progress..
So here is my question.
I'm trying to make a reflecting laser, this is my result so far.
From topview it looks kinda allright, but from the side view you can see that at point 1 and 2 the "Laser" is going towards the ground and I have no idea why. And It's not supposed to happen, I just want the laser to change from direction and not from height.

My Code:
     void Update () {
         Laser ();
     }
     
     void Laser(){        
         int maxVertCount = 5;       
         lineRenderer.SetVertexCount (maxVertCount);        
         RaycastHit hit;       
         Vector3 moveDir = Vector3.right;
 
         lineRenderer.SetPosition (0, transform.localPosition);             
 
         if(Physics.Raycast(transform.position,moveDir,out hit,Mathf.Infinity, layerMask)){     
         
             lineRenderer.SetPosition(1,hit.point);          
             lineRenderer.SetPosition (2, Vector3.Reflect((hit.point - transform.localPosition).normalized, hit.normal));            
 
 
             if(Physics.Raycast(hit.point, Vector3.Reflect((hit.point - transform.localPosition).normalized, hit.normal), out hit, Mathf.Infinity, layerMask)){
 
                 lineRenderer.SetPosition(3,hit.point);          
                 lineRenderer.SetPosition (4, Vector3.Reflect((hit.point - transform.position), hit.normal));    
 
             }
 
 
         }                                                       
       }
 
              Why are you using 'transform.localPosition' rather than 'transform.position' on lines 16 and 19. Seems to be that you want everything in world coordinates.
Answer by suribe · Mar 10, 2014 at 04:12 PM
You seem to be reflecting it always from each hit.point by using the gameObject.transform. I guess what you really want is to reflect on walls starting from each previous hit, right?
So you would actually need to do something along the lines of:
     void Laser() {
         int maxVertCount = 5;
         lineRenderer.SetVertexCount(maxVertCount);        
         RaycastHit hit;       
         Vector3 moveDir = Vector3.right;
 
         lineRenderer.SetPosition (0, transform.localPosition);
 
         Vector3 startPos = transform.position;
         for (int i = 1; i < maxVertCount; i++)
         {
             if (Physics.Raycast(startPos, moveDir, out hit, Mathf.Infinity, layerMask))
             {
                 lineRenderer.SetPosition(i, hit.point);
                 moveDir = Vector3.Reflect(moveDir, hit.normal);
                 startPos = hit.point;
             }
         }
     }
 
               The idea here is that you start each new laser segment from the hit point, and you set the new direction also according to how it has reflected. I have not tested the code, so it might not work without some changes, but it should give you some idea. It has the added benefit of using a loop, so you can make the lasers continue reflecting for as long as you want.
Thank you very much, this was exactly what I needed!
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Trying to make gameobject follow mouse cursor. It disappears instead. 2 Answers
Help in solving bug in my code :) 1 Answer
Line Renderer moves for no apperent reason 0 Answers
Distribute terrain in zones 3 Answers