Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by globalenemy · Oct 25, 2015 at 12:30 AM · instantiate

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;        
     }
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image globalenemy · Oct 24, 2015 at 11:58 PM 0
Share

populating the room was looking quite funny: http://gfycat.com/DependableSameBanteng

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image globalenemy · Oct 25, 2015 at 09:59 AM 1
Share

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

avatar image
0

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;



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

My code is instantiating many prefabs, i only want one. 1 Answer

When re-parenting instantiated object as child of current gameObject, why does GetComponents() not retrieve instantiated object's components? 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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges