Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Instantiate objects on the surface of a sphere?

Discussion in 'Scripting' started by parapsychic, Oct 10, 2020.

  1. parapsychic

    parapsychic

    Joined:
    Feb 15, 2016
    Posts:
    11
    [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?
     
    Last edited: Oct 11, 2020
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @parapsychic

    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.

    https://docs.unity3d.com/ScriptReference/Random-onUnitSphere.html
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You can calculate random points on a sphere: https://docs.unity3d.com/ScriptReference/Random-onUnitSphere.html
    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.
    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
     
  4. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    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.
     
  5. parapsychic

    parapsychic

    Joined:
    Feb 15, 2016
    Posts:
    11
    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!