Instantiate objects around a sphere
I made a little room and a character in my scene. Now that character felt pretty alone in his room, so I made a 'SpawnCharacter' script and a UI button to launch it. The room got full quite quick and so my character left the room and stepped on a planet. Now he's alone again, but my script for spawning him some friends doesnt work outside of the room. I need to make sure that the characters wouldn't spawn inside the planet. How would I do that?
My SpawnCharacter script for the room:
public class UIController : MonoBehaviour {
public GameObject spawnCharacter;
public Vector3 spawnOrigin;
public Vector3 spawnRange;
public void SpawnCharacter() {
Vector3 spawnPosition = new Vector3(
spawnOrigin.x + Random.Range(-spawnRange.x, spawnRange.x),
spawnOrigin.y + Random.Range(-spawnRange.y, spawnRange.y),
spawnOrigin.z + Random.Range(-spawnRange.z, spawnRange.z));
Quaternion spawnRotation = Quaternion.identity;
GameObject newCharacter = Instantiate(spawnCharacter,
spawnPosition, spawnRotation) as GameObject;
}
}
populating the room was looking quite funny: http://gfycat.com/DependableSameBanteng
Answer by MrMeows · Oct 25, 2015 at 03:13 AM
A simple application of Random.onUnitSphere should do the trick.
Vector3 spawnPosition = Random.onUnitSphere * (planetRadius + characterHeight * 0.5f) + planetPosition;
GameObject newCharacter = Instantiate(spawnCharacter, spawnPosition, Quaternion.identity) as GameObject;
newCharacter.transform.LookAt(planetPosition);
newCharacter.transform.Rotate(-90, 0, 0);
thank you very much sir! :)
if anyone's curious here's my final class:
public class SpawnOnPlanet : $$anonymous$$onoBehaviour {
public GameObject character;
public GameObject planet;
public void Run () {
Vector3 spawnPosition = Random.onUnitSphere * ((planet.transform.localScale.x/2) + character.transform.localScale.y * 0.5f) + planet.transform.position;
Quaternion spawnRotation = Quaternion.identity;
GameObject newCharacter = Instantiate(character, spawnPosition, spawnRotation) as GameObject;
newCharacter.transform.LookAt(planet.transform);
newCharacter.transform.Rotate(-90, 0, 0);
newCharacter.AddComponent<FauxGravityBody>();
newCharacter.GetComponent<FauxGravityBody>().attractor = planet.GetComponent<FauxGravityAttractor>();
}
}
If you are interested in using FauxGravity take a look at this: https://www.youtube.com/watch?v=gHeQ8Hr92P4
Answer by Flaming_Spirit · Jun 07, 2017 at 12:16 AM
Great answers above .... thank you to each and every contributor. I'd only like to add that if you're using "RaycastHit.point" information to spawn objects on the sphere (using mouse or touchscreen) just normalize the value using Vector3.Normalize()
Vector3 spawnPosition = Vector3.Normalize(raycastHit.point) * ((planet.transform.localScale.x/2) + character.transform.localScale.y * 0.5f) + planet.transform.position;
Your answer
Follow this Question
Related Questions
My code is instantiating many prefabs, i only want one. 1 Answer
Prefab destroyed upon instantiation 0 Answers
Instantiating a tile in place of another tile 0 Answers
Instantiate Object in camera View 0 Answers