Spawning Objects around a sphere

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.

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.

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.

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.

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?

Instantiate(meteor, Random.onUnitSphere, Quaternion.identity);

Will this work?

1 Like

Just tried it, and that does the trick. Thanks for your help!

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.

1 Like

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.

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.

1 Like