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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Spawning Objects around a sphere

Discussion in 'Scripting' started by Haxxy28, Nov 2, 2019.

  1. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Hi, just wondering if someone could help me on this. I've been trying to find tutorials as i'm new to coding but there is nothing on how to do this. Basically I have a planet and a meteor Prefab and want to spawn meteors around it. I have the gravity done, so I just want to know how to spawn objects around the planet. Thank you for your time.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Usually, you'd create a random vector with the length set to the distance from the origin of the planet, and then add that vector to thecenter of the planet to receive the coordinates for themeteor.

    Since this is so common, Unity has a method Random.OnUnitSphere that does most of thisfor you. You thensimply multiply the resulting vector with the desired distance, and add that to the planet's orign to receive the meteor's position.
     
  3. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    thanks, but I still don't really understand could you give me an example?

    I know that Random.onunitsphere exits but I couldn't find anywhere which showed me how to properly use it.
     
    Last edited: Nov 2, 2019
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It returns a number which lies on a sphere of size 1.

    To change the size multiply the result... this is your position for meteor spawning. It generates, as the documents say, a Vector3.

    A position is also a Vector3. Put the result into your spawn position.
     
  5. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    So I have the random position but I can't use it as its a Vector3 not a transform. How do I get around this?
     
  6. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    Code (CSharp):
    1. Instantiate(meteor, Random.onUnitSphere, Quaternion.identity);
    Will this work?
     
    Vinlow likes this.
  7. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Just tried it, and that does the trick. Thanks for your help!
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Learn how it all fits. Transform has a Vector3 position, a Quaternion rotation and so on. As it is, you will not be able to progress far like this without learning to code.
     
    Wilhelm_LAS likes this.
  9. Volyze

    Volyze

    Joined:
    Jul 2, 2020
    Posts:
    2
    Hello mate! May I ask as to if it's normal when I'm learning c# (I'm taking unity's basic course), It's still hard for me to try and create something like in the basic course on my own, even though I did it in the course.
    I just feel kinda demotivated and it makes me want to learn coding less cause it makes me think that I won't get far with coding.
     
  10. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    When I started I didn't really have any resources or materials. But I did know what I wanted to make. These days you'd want to have a goal in mind and just mod / steal / google the code needed to put it together.

    If you do that, it's a bit like learning another spoken language, it all just seems to start making sense and clicks. If it's all lesson after lesson, I can't see how it would motivate you.
     
    Wilhelm_LAS likes this.