Instantiate objects on the surface of a sphere?

[SOLVED]
Hi,
I’m pretty new to Unity Scripting. I’m working on a small game with the player having to walk around a spherical world and collect resources. I’ve done most of the other stuff, but I overlooked one very important and core feature: spawning resources (cubes) randomly on top of this spherical world.

As I said, my world is a sphere. I’m trying to instantiate objects on it’s surface. It should be randomly spawned. I can handle a while loop to make multiple of them but I can’t seem to figure out the calculation to spawn it on this sphere’s surface randomly. Most answers seem to suggest Raycasts, but I’ve got no idea what a raycast is. Any other way to spawn it on this surface?

@parapsychic_1

Well if your world is actually a sphere (and not something close to it with hills and such) you could use random on unit sphere to pick points (multiply the result vector with your planet radius).

After creating this random position maybe do some check to see if there is anything that would block placing object there to avoid overlapping objects.

You can calculate random points on a sphere: Unity - Scripting API: Random.onUnitSphere
Scale it up to your world size and above. Then do a downwards raycast to determine the actual position to place the object at (in case the sphere is bumpy, if it’s an actual sphere you should only need Random.onUnitSphere and a scale).

Raycasts are what they sound like. You cast a ray from some origin in some direction. Imagine a line starting at some point and being drawn in some direction. In your case, starting at a random point above your sphere world and going downwards. You then want information about the location it hits. That’s what the result provides.

If the world is sphere. You should first know the centre of that sphere (world).
Then the instantiated object must be located at radius R of sphere from the centre of sphere. And you only need to randomise the unit-direction in which the instantiation will occur.

Thank you guys. @eses @Yoreki @VishwasGagrani
This was exactly what I was looking for. And since I’ve got a gravity script on the prefab, I can safely spawn it in a sphere of radius, Radius of world < Radius < Gravity escape distance. That way, it’ll account for the small irregularities. Thanks again!