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 Saif Mohamd · Feb 28, 2013 at 10:20 AM · objectrandomgeneration

Generate an object in random positions ?

Hello,

I tried to generate an object in random positions but it didn't work good, it was generating very fast, i didn't know why? also the rotation is generating in weird way, I wanted all the rotation as the original object, I used this code:

 #pragma strict
 
 var theObject:Transform;
 var theNewPos:Vector3;
 var maxPos:int=2000;
 var minPos:int=9;
 var max=1;
 var i = 0;
 
 
 
 
 function Start(){
 
 
 }
 
 
 function Update(){
 
 
 
 for(i=0;i<max;i++)
 {
 
 spawn();
 }
 
 
 }
 
 
 function spawn() {
 
 
 yield(WaitForSeconds(2));
 theNewPos= new Vector3 (Random.Range(minPos,maxPos),0,Random.Range(minPos,maxPos));
 Instantiate(theObject,theNewPos,transform.rotation);
 
 
 
 }



Thanks.

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Feb 28, 2013 at 10:41 AM

Here is a rewrite of your code. I made a lot of changes which I usually don't like to do.

 var theObject:GameObject;
 var maxPos:float = 2000.0;
 var minPos:float = 9.0;
 var max = 22;
 
 function Start(){
     StartCoroutine(spawn());
 }
 
     function spawn() : IEnumerator {
         for (var i = 0; i < max; i++) {
             yield WaitForSeconds(2.0);
             var theNewPos= new Vector3 (Random.Range(minPos,maxPos),0,Random.Range(minPos,maxPos));
             var go : GameObject = Instantiate(theObject);
             go.transform.position = theNewPos;
         }
     }

A few comments:

  • Positions and positions in Unity are float values. While int variables can be converted, there are some bugs that pop out when using ints instead of floats.

  • If you call Instantiate() without parameters, it uses the position and rotation of the original object. Then on the next line I set the position, but left the rotation alone.

  • Your method of trying to spawn one every 2 seconds would not work. You could have done a timer implemented in Update() rather than the solution I have here.

Comment
Add comment · Show 5 · 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 Saif Mohamd · Feb 28, 2013 at 10:47 AM 0
Share

Thanks alot, I don't know all unity commands and functions, I programmed alot before but not in unity, would suggest me some good sites that explains the functions? thanks.

avatar image Chronos-L · Feb 28, 2013 at 10:48 AM 0
Share

@robertbu, do you $$anonymous$$d explaining why the yield() in his script will not work. Does it have something to do with the StartCoroutine()?

avatar image robertbu · Feb 28, 2013 at 11:05 AM 0
Share

I think this is how it works. I don't have as strong a foundation with the IEnumerator/yield as I would like:

Let's assume the app was running at 60 fps and we were getting 60 Update() calls per second as well. The original code was calling spawn() once per frame (max was == 1). Each call generated a suspended version of the spawn(), so there were 120 suspend method calls in 2 seconds. At the end of 2 seconds the first one fired, and at 2 + 1/60 the second one would fire, and at 2 + 2/60 the third one would fire. So the code had a 2 second pause and the a whole slew of game object would be generated at the rate of one per frame. Of course, the code was continuing to call spawn at the rate of once per frame keeping 120 calls stacked up and waiting to go.

avatar image robertbu · Feb 28, 2013 at 11:10 AM 0
Share

@Saif $$anonymous$$ohamd: @Julien.Lynge includes a list of resources in many posts. I've spent time with some of them.

Take a look at this answer:

http://answers.unity3d.com/questions/378740/where-can-i-begin-to-learn-how-to-make-an-inventor.html

avatar image Bug84 · May 06, 2016 at 11:15 AM 0
Share

@robertbu, Hey guys i am new in unity script.Robertbu thanks for your script.its working fine. But how can i create those clone object in random position. Actually someone wants to test my unity skills. So they offer me a project. Project description is like this:- (1) Create a ground (2) generate some Sphere (Sphere are randomly pop out from ground or some other game objects in random position/time and move toward the sky and destroy automatically after few seconds) . Before they destroy automatically have to shoot them. Now i can create the shooter and bullets. But i am struggling with the random Sphere creation procces.Please help me.

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Yield after spawning object 1 Answer

Tree position on random Terrain (y axis) 0 Answers

Make sure objects are not instantiated on each other 2 Answers

Randomly Generated Levels 3 Answers

How to create a perlin noise heightmap with the values being the highest around a point? 1 Answer


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