Random.onUnitSphere not working.

Hello, i am making a game and i use Random.onUnitSphere to spawn metreots at random postions.
but theproblem is when i run my game the metreots spawns at one locaction.
Can somebody help me with this promblem?

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeteorSpawner : MonoBehaviour
{

public GameObject MeteorPrefab;

void Start ()
{
StartCoroutine(SpawnMeteor());

}
IEnumerator SpawnMeteor()
{
Vector3 postion = Random.onUnitSphere * 200;

Instantiate(MeteorPrefab);

yield return new WaitForSeconds(1f);

StartCoroutine(SpawnMeteor());
}
}

I don’t see you actually setting the position after instantiating the meteor.

1 Like

Yes, you’re right, it should be something like

Instantiate ( MetorPrefab, postion.transform.position, Quaternion.Identity );

Also, please use code tags OP to make things easier in the future, alternatively if you want gameobjects to be placed directly on the surface of the sphere you’ll probably want some rotational code as well but I’ve forgotten how to do that.

I remember it having something to do with normalize or normals though?

OnUnitSphere just gives you a random Vector placed on the surface of an imaginary sphere that is 1 unit in radius. So you’d just pass position as the second parameter.
https://docs.unity3d.com/ScriptReference/Random-onUnitSphere.html

1 Like

Ah, my bad, thanks.