Why does Raycasthit never actually hit anything?

Like the title says, my Raycasthit never hits anything. Can anyone tell me why? I’ve never worked with them before so little fuzzy here and there…

using UnityEngine;
using System.Collections;

public class CameraFader : MonoBehaviour
{
	private Vector3 vec;
	RaycastHit hit = new RaycastHit();
	Ray ray = new Ray();
	void LateUpdate()
	{
		vec.x = (float)Screen.width / 2;
		vec.y = (float)Screen.height / 2;
		vec.z = 0f;
		ray = GameController.GameScript.cam.camera.ScreenPointToRay(vec);
		//ray = camera.ScreenPointToRay(vec);
		
		Vector3 start = GameController.GameScript.cam.transform.position;
		Vector3 end = GameController.GameScript.cameraTarget.transform.position;
		float distance = Vector3.Distance(start, end);
		
				
		if(Physics.Raycast(ray, out hit, distance))
		{
			Debug.DrawLine(ray.origin, hit.point, Color.red);
			Debug.Log ("hit!");
		}
		else
		{
			Debug.Log ("hit nothing");
		}
	}
}

Is “GameController” Assigned?
Log the ray and distance before casting to see if they are what you expect them to be.
Is there definatly something between start and end?

I think you can just tell the raycast the distance with a float in the line.
You’re using late update because the target is moving?

raycast hits return data about an object that is hit. The object must have a collider assigned, make sure the collider is correctly placed with the graphic. You can use a layermask to prevent the ray from going through certain things. Check the unity reference manual for a code example.

I ran your script, it works, you need to look at whats going on in the scene with colliders, layermasks and such…
Make sure your target is at the direct center of the camera. If its a little off, use a spherecast.

I attached your script to the main camera in a little scene.

using UnityEngine;
using System.Collections;

public class CameraFader : MonoBehaviour

{
	public GameObject target;
    private Vector3 vec;

    RaycastHit hit = new RaycastHit();

    Ray ray = new Ray();

    void LateUpdate()

    {

        vec.x = (float)Screen.width / 2;

        vec.y = (float)Screen.height / 2;

        vec.z = 0f;

        ray = camera.ScreenPointToRay(vec);

        //ray = camera.ScreenPointToRay(vec);

        

        Vector3 start = transform.position;

        Vector3 end = target.transform.position;

        float distance = Vector3.Distance(start, end);

        

                

        if(Physics.Raycast(ray, out hit, distance))

        {

            Debug.DrawLine(ray.origin, hit.point, Color.red);

            Debug.Log ("hit!");

        }

        else

        {

            Debug.Log ("hit nothing");

        }

    }

}

I’ll have to have a look then, but there was definitely a collider on the object and it was never ‘hitting’ anything.

Debug.DrawLine this may be helpful for debugging if you see a visual intersection there might be something wrong with your colliders otherwise you may see if your raycast is pointing to the wrong direction.

Are you raycasting from the center of a separate sphere collider? Doing that will automatically return false.