Raycast not calculating correctly (doesn't go to mouse position)

The raycast coming from the gameobject and going to the hit point is not going to the position of the mouse. The linerenderer works as it should.

PlayerController Script

		if(Input.GetButton("Laser")){
			
			RaycastHit hit;
			Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			if (Physics.Raycast (ray, out hit)) {
				mousePoint = hit.point;
			} else {
				mousePoint = new Vector3 (emitter.transform.position.x, emitter.transform.position.y, 100f);
				print ("no mousepoint");
			}

			Debug.DrawRay (emitter.transform.position, mousePoint, Color.red);

			Physics.Raycast(emitter.transform.position, mousePoint, out targetHit); 
			if (targetHit.collider != null) {
				target = targetHit.collider.gameObject;
				print (target.gameObject.name);
			} 

		
		}//end laser

LineRenderer Script

	// Use this for initialization
	void Start () {

		lineRenderer = GetComponent<LineRenderer> ();
		player = GameObject.FindGameObjectWithTag ("Player");


		lineRenderer.SetPosition (0, emitter.transform.position);
		lineRenderer.SetWidth (.05f, .05f);


	}
	
	// Update is called once per frame
	void Update () {
		Vector3 mousePoint = player.GetComponent<PlayerController> ().mousePoint;

		lineRenderer.SetPosition (1, mousePoint);

	}
}

DrawRay takes one worldspace position and a worldspace direction. However you pass two positions. So either use DrawLine which takes two positions or pass the ray direction as second parameter.

To be more precise you might also want to use they ray’s origin as that’s the actual starting point of the ray:

Debug.DrawRay(ray.origin, ray.direction * 5f, Color.red);

Note: the direction is usually a normalized direction (a vector with length 1.0). So you have to multiply the direction with your desired length when you visualize it with DrawRay.

To anyone else having this issue, this answer was very useful!

I switched from using RayCast as above to using

RaycastHit2D hit = Physics2D.Linecast(origin, direction, whatToHit);

And it works smoothly now, Thanks Bunny!