Read objects from left to right

Viewed 309

I wrote a C# Script in Unity, which detects all visible objects. Now I would like to output them from left to right, as how they are positioned in the scene. Each objects has a number and yet the script outputs the objects by ascending order.

My idea:

I thought about a for-loop which goes along the horizontal field of view. First calculating the horizontal FOV by:

private static float horizontalFOV() {
    float radAngle = Camera.main.fieldOfView * Mathf.Deg2Rad;
    float radHFOV = 2 * Mathf.Atan(Mathf.Tan(radAngle / 2) * Camera.main.aspect);
    float hFOV = Mathf.Rad2Deg * radHFOV;

    return hFOV;
}

And creating the loop by:

public static string OutputVisibleRenderers (List<Renderer> renderers) {
    float hFov = horizontalFOV ();

    if (null == renderers)
        throw new System.ArgumentNullException ("renderers are null");

    for (int i = 0; i < hFov; i++) {
        foreach (var renderer in renderers) {
                if (IsVisible (renderer)) {
                myList.Add (renderer.name);
            }
        }
    }
    string itemsInOneLine = string.Join ("-", myList.ToArray());
    myList.Clear ();
    print (itemsInOneLine);
    print ("--------------------------------------------------");
    return itemsInOneLine;
}

But unfortunally this doesn't work. So how could I read all objects from left to right?

2 Answers
Related