Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by g181 · Jul 05, 2014 at 07:54 PM · spawnprefabsobjectssphere

Spawn Random Objects on a Sphere Surface

Hi all that my first Question on UnityAnswer :) My problem as i said before is spawn randomly objects on a sphere surface in movement. i have 2 script started one of them work but didnt spawn objects random the other one didnt work and is intended to spawn them randomly( i found the secondo one online and guys who posted it said it worked for them but for me no).

1-

public GameObject ObjectPrefab; public int NumberOfPrefab; //public float Scale; private bool first = true;

 [ContextMenu("Create Points")]
 void Update () {
     if ((Network.peerType == NetworkPeerType.Client || Network.peerType == NetworkPeerType.Server) && first==true) {
         var points = UniformPointsOnSphere (NumberOfPrefab,  (float)(transform.localScale.x)); // Scale
         for (var i=0; i<NumberOfPrefab; i++) {
             var g = Instantiate (ObjectPrefab, transform.position + points [i], Quaternion.LookRotation(transform.position + points [i]-transform.position)*Quaternion.FromToRotation(Vector3.up, transform.forward)) as GameObject;
             g.transform.parent = transform;
             //print (i + " of " + NumberOfPrefab + "\n");
         }
         first=false;
         //print ((float)(transform.localScale.x));
     }
 }
 
 Vector3[] UniformPointsOnSphere(float N, float scale) {
     var points = new List<Vector3>();
     var i = Mathf.PI * (3 - Mathf.Sqrt(5));
     var o = 2 / N;
     for(var k=0; k<N; k++) {
         var y = k * o - 1 + (o / 2);
         var r = Mathf.Sqrt(1 - y*y);
         var phi = k * i;
         points.Add(new Vector3(Mathf.Cos(phi)*r, y, Mathf.Sin(phi)*r) * scale);
     }
     return points.ToArray();
 }

2-

int numberOfRuins = 100; int ruinCount; float minOriginDistance = 1.0f; RaycastHit hit; public GameObject ObjectPrefab;

 void Start(){
     while (ruinCount < numberOfRuins){
         // cast a random ray to see if we hit land
         Vector3 randomVec = Random.onUnitSphere*(float)(transform.localScale.x);
         if (Physics.Linecast(randomVec * 2, transform.position, out hit, 1<<8)){
             if (hit.point.magnitude > minOriginDistance){
                 // we hit land!
                 var obj = Instantiate(ObjectPrefab, hit.point, Quaternion.identity)as GameObject;
                 obj.transform.LookAt(hit.point + randomVec);
                 ruinCount++;
             } 
         }
     }
 }

can u guys help me to understand why the second one didnt work ? ty

Comment
Add comment · Show 2
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 g181 · Jul 07, 2014 at 07:15 AM 0
Share

up pls :) ty

avatar image robertbu · Jul 07, 2014 at 07:39 AM 0
Share

I'm not sure enough of what you are doing for an answer, but I do spot a couple of strange things. First when you do the Raycast():

  if (Physics.Linecast(randomVec * 2, transform.position, out hit, 1<<8)){

The position you are using (randomVec) only works if your object is at (0,0,0). If your sphere is at some place other than the origin, then you would need to do:

 if (Physics.Linecast(transform.position + randomVec * 2, transform.position, out hit, 1<<8)){ 

This line is also a bit problematic (though not your issue):

 Vector3 randomVec = Random.onUnitSphere*(float)(transform.localScale.x);  

'localScale.x' is the diameter of a standard sphere, not the radius.

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

I need To spawn game objects randomly, but I'm Getting an error in My script, Please help! 1 Answer

Unity2d : Spawning in Prefabs 1 Answer

Destroy The Same Objects Before Instantiating 2 Answers

How to rotate snake at an exact 90 degrees 2 Answers

Making Object Clickable(Need Guidance ASAP) 2 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