Using a range of raycasts with varying angles.

I’ve been trying to use Raycasts to scan a portion of a 3d sphere for ground detection but I can’t seem to get the angle to work correctly (mainly because I’m just guessing at this point).

The picture below kind of shows what I’m trying to do, with the angle between the down direction and the highest raycast being adjustable. Except on a 3d Sphere.

If you guys have any tips on how I can achieve this I’d love to hear them.

https://forum.unity.com/threads/evenly-distributed-points-on-a-surface-of-a-sphere.26138/
this might help. just discard all points above a certain threshold

I wrote a code, because I was interested in this topic.

// These are the resolutions, odd numbers are favourable
public int xCastNum = 35;
public int zCastNum = 35;

// Max angle (transform.down is 0° in this case)
float maxAngle = 90;

// Variables so you can use them later
int minX;
int maxX;

int minZ;
int maxZ;

float angleMultiplierX;
float angleMultiplierZ;

private void Start()
{
    RecalcX();
    RecalcZ();
}

private void Update()
{
    for(int x = minX; x < maxX; x++)
    {
        for (int z = minZ; z < maxZ; z++)
        {
            Vector3 direction = Quaternion.AngleAxis(x * angleMultiplierX, transform.right) * Quaternion.AngleAxis(z * angleMultiplierZ, transform.forward) * -transform.up;
            Debug.DrawLine(transform.position, transform.position + direction);
        }
    }
}

void RecalcX()
{
    minX = -xCastNum / 2;
    maxX = -minX + 1;

    angleMultiplierX = maxAngle / (xCastNum / 2);
}

void RecalcZ()
{
    minZ = -zCastNum / 2;
    maxZ = -minZ + 1;

    angleMultiplierZ = maxAngle / (zCastNum / 2);
}

public void ChangeX(bool add)
{
    if (add) xCastNum++;
    else xCastNum--;
    RecalcX();
}

public void ChangeZ(bool add)
{
    if (add) zCastNum++;
    else zCastNum--;
    RecalcZ();
}

I was not interested in even numbers, so if you need it, you have to implement it yourself. I added functions so you can link buttons to them and see how they work. I used Debug.Drawline to see the results.

If you want to make it rotation-independent, use Vector3.right and Vector3.forward.

I buckled down and figured something out that works while still giving me some pretty evenly spaced points.

The “angle” variable defines the maximum angle that the raycasts take, and the “resolution” is how many pieces that range is broken up into. so and angle of 45 and a resolution of 9 means the rays go in steps of 5 degrees.

It seems efficient enough for my use, but I did test it with 45 degree angles with a resolution of 45 and it just tanks, so there’s probably some improvements I could make.

		float resolution = 9f;
		float angle = 45f;
		float radius = 0.5f;

		float Y, X, Z, tempAngle, tempAngle2;
        Vector3 direction;
        Vector3 center = transform.position;

        
        for(tempAngle = 0; tempAngle <= angle; tempAngle += angle / resolution)
        {
            Y = -Mathf.Cos(tempAngle * Mathf.Deg2Rad);

            for (tempAngle2 = 0; tempAngle2 <= 180; tempAngle2 += angle / resolution)
            {
                X = Mathf.Cos(tempAngle2 * Mathf.Deg2Rad) * Mathf.Sqrt(1 - Mathf.Pow(Y, 2));
                Z = Mathf.Sqrt(1 - Mathf.Pow(Y, 2) - Mathf.Pow(X, 2));

                direction = Vector3.Normalize(new Vector3(X, Y, Z));
                Debug.DrawRay(center, direction * (radius), new Color(X, Y, Z, 1));

                direction = Vector3.Normalize(new Vector3(X, Y, -Z));
                Debug.DrawRay(center, direction * (radius), new Color(X, Y, Z, 1));
            }

        }