Raycasting with the origin of the ray coming from the cursor

Hello, I have a player with a movement script attached to it, a camera child to the player with a mouselook script attached to it.

Then I attached this raycasting script to it, I want the ray to move when I move the camera with the mouselook script, so that the ray basically has its origin point in the center of the screen, and the direction changes when the camera changes.

using UnityEngine;
using System.Collections;

public class gg : MonoBehaviour {

	public float distance = 5;

	// Use this for initialization
	void Start () {


	
	}
	
	void Update() 
	{

		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

		if (Physics.Raycast (ray, distance)) 
		{
			print ("Yes");
		} 

		else 
		{
			print ("No");
		}

		Debug.DrawRay (ray.origin, ray.direction * distance, Color.red);
	}
}

I get this error: ‘NullReferenceException: Object reference not set to an instance of an object’ on line 18, (Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition):wink:

Tysm for helping!

You probably don’t need to use the mouse position if your ray is always coming from your view center.

Consider building your ray like this:

	Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f) );

This sets the origin to be the center of the camera’s view, outward.

Then, as long as you have a main camera, and as long as your distance in the raycast is enough, you shouldn’t have any problems.

EDIT:
I may have the wrong idea about what you are trying to do.
Your original script causes a ray to come from the centerpoint of the camera down the mouse position into the world, and will have an angled effect like looking down the ray as a barrel, so it won’t be visible. If you want the ray to come directly from the center at all times, the mouse position isn’t necessary, but the ray will still not be visible to the camera (still looking down it).

Alternative rays include casting from a world mouse position, to a position using the camera’s forward axis. This is not often used, in my experience.:

Vector3 mouseScreen = Input.mousePosition;
            // you must define a starting plane away from the camera for this to work.
            // this essentially specifies the 'radius' of the mouse's influence.
		mouseScreen.z = 0.5f;  
		Vector3 rayOrigin =  Camera.main.ScreenToWorldPoint( mouseScreen );

            // use the camera's forward direction
		Vector3 rayDirection = Camera.main.transform.TransformDirection(Vector3.forward);

This will cause rays to follow the mouse but always be parallel to the camera’s z axis.

If you are instead looking for a ray from the camera’s center to a specific world depth, instead of looking down a ray like your original code, you need to specify a world position for the ray’s end point.

Vector3 mouseScreen = Input.mousePosition;
		mouseScreen.z = 0.5f;
		Vector3 rayDest = Camera.main.ScreenToWorldPoint( mouseScreen);
		
		// ray starts at camera center
		Vector3 rayOrigin =  Camera.main.transform.position;
		
		// ray goes to the mouse position a specific distance from the camera.
		Vector3 rayDirection = rayDest - rayOrigin;

		Ray ray = new Ray(rayOrigin, rayDirection);

This is another less usual version, where you are specifying a second point in space to draw the line to from the camera’s center, based on the position of the mouse.

Hopefully one of these is what you’re looking for.

The first script did exactly what I needed, ty! I also understand now why I need to use ‘ViewportPointToRay’ instead of ‘ScreenPointToRay’. Ty man!

Alexander