C# Get Rays that miss in Raycast

Hi guys,
I am wondering if its possible to have access to the Rays that don’t hit any objects in the scene, what would be the best option for this?

Thank you

Nicholas

float maxDiatance;
Ray missedRays;

void Update()
{
    Ray rndRay = new Ray ((Random.OnUnitSphere() - transform.position).normilized);
    if (!Physics.Raycast (rndRay, maxDistance, Physics.AllLayers)) // not sure about params order here
    missedRays.Add (rndRay);
}

Hi Ermiq,
Thank you for your reply,

I am still getting the same output as I did prior to writing the post, when I try to get access to the missing rays, in this case shown in yellow, they all point to a strange direction as shown in the first image and not as how It should be expected to be displayed like in the second image.

Here is the version of my code

List missedRays = new List();
public void Update()
{

    foreach (var direction in UniformSphericalPointDistribution(200)) // UniformSphericalPointDistribution distributes uniformly  n points on a sphere
    {
        RaycastHit hit;
        if (Physics.Raycast(this.transform.position,direction,out hit, Mathf.Infinity,Physics.AllLayers))
            Debug.DrawLine(this.transform.position, hit.point, Color.red);

        else
        {
            missedRays.Add(direction);
        }
      
    }

    foreach (var direction in missedRays)
    {
        Debug.DrawLine(this.transform.position, direction, Color.yellow);

    }

}

Hi, Debug.DrawLine needs two parameters that are the Start and the End point. In your yellow lines you used the Start point and then the Direction that isn’t the same thing as the End point.


If you would use a direction instead an End point use Debug.DrawRay:

Debug.DrawRay(this.transform.position, direction, Color.yellow);