Random.insideUnitSphere Help

Please guys i need to instantiate over 360° on the sphere … i try everything…

using UnityEngine;
using System.Collections;

public class RandomlyPlaceObjectsOnSurface : MonoBehaviour
{
    public GameObject[] objectsToSpawn;
    public float spawnRadius;
    public int numberOfObjects = 10;
    public bool randomOrientation = false;
    public bool orientToSurface = false;

    // Use this for initialization
    void Start ()
    {

        //spawnRadius = Random.Range(0f, 360f);

        for (int i = 0; i < numberOfObjects; i++)
        {
            //What we will spawn
            GameObject objectToSpawn = objectsToSpawn[Random.Range(0,objectsToSpawn.Length)];

            Vector2 spawnPositionV2 = Random.insideUnitSphere * spawnRadius;

            Vector3 spawnPosition = new Vector3(spawnPositionV2.x,0,spawnPositionV2.y);

            Vector3 transformOffsetSpawnPosition = transform.position+spawnPosition;

            RaycastHit hit;
            if(Physics.Raycast(transformOffsetSpawnPosition,Vector3.down,out hit))
            {
                Vector3 finalSpawnPosition = hit.point;

                Quaternion orientation;
               
                if(randomOrientation)
                {
                    orientation = Random.rotation;
                }
                else if(orientToSurface)
                {
                    orientation = Quaternion.LookRotation(hit.normal);
                }
                else
                {
                    orientation = objectToSpawn.transform.rotation;
                }

                Instantiate(objectToSpawn,finalSpawnPosition,orientation);
            }
        }
    }
}

You seem to be creating a 2d circle and then raycasting down from above - that will give you some odd results and you’ll end up with most objects bunched around the top (and none at the bottom).

You could do this:

Vector3 spawnNormal = Random.insideUnitSphere;
Vector3 spawnPosition = spawnNormal * ( spawnRadius + 0.5f );

This would create a position above the sphere, you would then use the reverse of that normal as the raycast direction.

if(Physics.Raycast(transformOffsetSpawnPosition,-spawnNormal,out hit))
1 Like

That shuldn’t be too difficult a problem with polar coordinates. r is fixed (half the Diameter of the sphere, Theta and phi (the two angles that place the Point) are randoms between 0 and 2 * Pi.

And so we can calculate x, y and z thusly:

Theta = Random.Range(0, 2f*Mathf.Pi);
phi = Random.Range(0, 2f * Mathf.Pi);

r = 1f; // or whatever your radius of sphere is
x = r * sin(Theta) * cos(phi);
y = r * sin(Theta) * sin(phi);
z = r cos(Theta);

You now have a Point on the surface of a sphere with a Diameter of 2 * r (2.0 in this case), centered around the origin (0,0,0).

The normal at this Point is aligned woth the it’s own vector from the origin (make it’s ‘up’ Point in the same direction as it’s coordinates, only normalize the vector).

To make objects appear above or below the surface of the sphere, increase (above) or reduce (below) r.

This may explain what polar coordinates are in case you are interested