Hello,
I need to get a few objects to spawn at a location away from the player at a distance of 250 and on a sphere.
Using Random.InsideUnitSphere takes care of the location. But the problem is I cant think of an efficient way to limit the position of my object to the outline of the sphere.
I am working on VR for Cardboard so performance is a must, the only thing I can think of is generating a position and checking if that one is on the outer layout, if not generate again (using a while loop to check the values…, but this has proven to be very inefficient)
void Update ()
{
playerPosition = GameObject.Find("Head").transform.position;
if (amountOfObjectCreated < amountOfObjectsToCreate)
{
InstantiateRandom();
}
}
void InstantiateRandom()
{
Vector3 sphericalPosition = Random.insideUnitSphere * 250;
//Constrain position
sphericalPosition.y = Mathf.Abs(sphericalPosition.y);
Vector3 objectPos = sphericalPosition + playerPosition;
GameObject temp = Instantiate(objectToCreate, objectPos, Quaternion.identity) as GameObject;
temp.AddComponent<GazeImproved>();
amountOfObjectCreated++;
}