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));    

			}


		}                                                       
      }

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.