beam weapon not working as intended

I’m making a top down 2d shooter in which there is a beam weapon I found a tutorial and followed it however the output isn’t quite what I was hoping for.

the beam is supposed to shoot at the mouse position however here it is shooting at the origin point (0,0)

how do I make the beam shoot at the mouse?

here is the code:

using UnityEngine;
using System.Collections;

public class beamWeapon : weaponObject {

	RaycastHit hit;

	float range = 100.0f;

	LineRenderer line;

	public Material lineMat;

	Ray ray;

	void Awake()
	{
		line = gameObject.GetComponent<LineRenderer> ();
		line.SetVertexCount (2);
		line.material = lineMat;
		line.SetWidth (0.1f, 0.25f);
	}

	void Update()
	{
		ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	}

	public override void FireWeapon()
	{
		Physics.Raycast (ray, out hit, range);

		line.enabled = true;
		line.SetPosition(0, transform.position);
		line.SetPosition(1, hit.point + hit.normal);

	}

	public override void stopFiring ()
	{
		line.enabled = false;
	}
}

Here’s the link to the same problem you are having. It’s for ScreenToWorldPoint(), but it’s the same for ScreenPointToRay

If all of your objects have zero for positions z value, instead of 26 line you could use this:

 Camera.main.ScreenPointToRay (new Vector3(
     Input.mousePosition.x,
     Input.mousePosition.y,
     -Camera.main.transform.position.z));