Line Renderer. How to lock z axis for side scroller?

I’m making a side scrolling platformer/shooter and want the player to fire a laser when the left mouse button is held and the laser to follow the mouse (twin stick shooter type controls). The line should origin from the players gun and to infinity, unless it hits collision or an enemy.

I sourced the following code, which I attached to the player, which sort of works for mockup visualisation, but if it doesnt hit any assets it wont render, and if there are assets on a different value along the z axis, i.e. background decorations, when I click the line will render from the player to that asset (forgive the indentation, I’m new to scripting and trying my best):

#pragma strict
 
@script RequireComponent (LineRenderer)
 
 
var mouse : Vector2;
var hit : RaycastHit;
var range : float = 100.0;
var line : LineRenderer;
var lineMaterial : Material;
var ray : Ray;
 
function Start()
{
line = GetComponent(LineRenderer);
line.SetVertexCount(2);
line.renderer.material = lineMaterial;
line.SetWidth(0.1f, 0.25f);
}
 
function Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, range))
{
if(Input.GetMouseButton(0))
{
line.enabled = true;
line.SetPosition(0, transform.position);
line.SetPosition(1, hit.point );
}
else
line.enabled = false;
}
 
}

I want the line to render regardless, and be locked to 0 on the z axis, the same as the player. Any help would be greatly appreciated. I have searched extensively for answers but not found anything specific.

On line 30, insert:

hit.point.z = 0.0;

Or alternately if you are going to use hit.point;

var v3 = hit.point;
v3.z = 0.0;
line.SetPosition(1, v3);