The question is answered, right answer was accepted
Random.Onunitsphere confusing different spheres
Hello guys,
I am building a game where there are two planets made out of 3d Spheres. Both of them exist on a single scene. They are very small planets and i've used the theme of small planets. On the planet where the game starts , i've used Random.onUnitSphere to randomly spawn gameobjects that the player can collect. After a certain objective is completed, the player is transported to the second planet where the player also has to collect gameobjects that are spawned randomly on it's surface.There too, i tried using Random.onUnitSphere on the separate script and attached it to the second planet to spawn gameobjects but they all end up spawning on the first planet. I cannot figure out why the Random.onUnitSphere is spawning only on the first planet and not on the second. And to clarify, i've used different scripts on each planet to spawn different gameobjects. This is the code for the Script i've used on the second planet.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawnplanet2 : MonoBehaviour
{
public GameObject spawnPowerupPlanet2;
//public GameObject spawnparticlePlanet2;
// Use this for initialization
void Start()
{
StartCoroutine(spawnPowerPlanet2());
}
// Update is called once per frame
void Update()
{
}
IEnumerator spawnPowerPlanet2()
{
Vector3 placePlanet2 = Random.onUnitSphere * 7.4f;
yield return new WaitForSeconds(2.0f);
Instantiate(spawnPowerupPlanet2, placePlanet2, Quaternion.identity);
StartCoroutine(spawnPowerPlanet2());
}
}
and this is the one i've used on the first planet . I've repeated some lines in this one , i'll figure a way out to reduce redundancy later, :D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class powerupsspawn : MonoBehaviour {
public GameObject spawnPowerTrap;
public GameObject spawnPowerup;
// private GameObject power;
public GameObject spawnparticle;
public Text healthText;
float f;
// Use this for initialization
void Start ()
{
StartCoroutine(spawnPower());
// StartCoroutine(SpawnandDestroy(spawnPowerup,5f));
}
IEnumerator spawnPower()
{
Vector3 place = Random.onUnitSphere * 7.4f;//7 is the distance on how far you want the spawn to happen
Vector3 place2 = Random.onUnitSphere * 7.4f;
Vector3 place3 = Random.onUnitSphere * 7.4f;
Vector3 place4 = Random.onUnitSphere * 7.4f;
Instantiate(spawnparticle, place, Quaternion.identity);
Instantiate(spawnparticle, place3, Quaternion.identity);
Instantiate(spawnparticle, place4, Quaternion.identity);
Instantiate(spawnparticle, place2, Quaternion.identity);
// Vector3 pickuposition = spawnPowerup.transform.position;
//yield return new WaitForSeconds(3.5f);
yield return new WaitForSeconds(2.0f);
GameObject spawned = Instantiate(spawnPowerup, place, Quaternion.identity);//the position takes a vector3 value so "place" is a random vector
GameObject spawned1 = Instantiate(spawnPowerup, place2, Quaternion.identity);
GameObject spawnedTrap2 = Instantiate(spawnPowerTrap, place3, Quaternion.identity);
// GameObject spawnedTrap = Instantiate(spawnPowerTrap, place2, Quaternion.identity);
GameObject spawnedTrap = Instantiate(spawnPowerTrap, place4, Quaternion.identity);
Destroy(spawned,1.5f);//destroys spawned gameobject sater 3 seconds
Destroy(spawned1, 1.5f);
Destroy(spawnedTrap2, 4f);
Destroy(spawnedTrap, 4f);
StartCoroutine(spawnPower());//loops the spawning
}
// Update is called once per frame
void Update () {
healthText.text = "Health :" + GameObject.Find("Player").GetComponent<PlayerMovementScript>().health;
}
}
Answer by muttsang · Sep 24, 2018 at 11:59 PM
no problem folks :D I found a solution i used this i.e added the position of the second planet from the script .
Vector3 placePlanet2 = gameObject.transform.position + Random.onUnitSphere * 7.4f;