• 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 jaxsonp10 · Feb 17, 2014 at 11:42 PM · randomobjectsgenerationarea

Randomly Generated Objects inside an area

I need a code to spawn in multiple objects in specified area when the scene is first loaded. I'm trying to make a island resource survival game kinda like the game "Stranded" or "Stranded 2", And I need to spawn in t$$anonymous$$ngs like trees/rocks in a certain part of the island, but still at random spots with a random number of t$$anonymous$$ngs. If you do submit a script, I would like you to please explain what everyt$$anonymous$$ng means and what everyt$$anonymous$$ng does so I can change t$$anonymous$$ngs if need be, If that wouldn't be that hard for you guys? I haven't messed with t$$anonymous$$ngs like t$$anonymous$$s before, and I want to learn what I'm doing. any help would be much appreciated.

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 thaiscorpion · Feb 18, 2014 at 12:14 AM 0
Share

What is the area like you want to spawn things in? is it round or squared? and do you want the things on a grid like layout or totally random?

avatar image jaxsonp10 · Feb 18, 2014 at 02:41 AM 0
Share

Well, either is fine really, and totally random.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by erick_weil · Feb 18, 2014 at 12:22 AM

try by creating a function and calling then in start()

make a area Vector3 paramater and a GameObject for the Instance.

 function generateisland(NumObjects : int,wichobject : GameObject,area : Vector3)
 {
 .
 .
 .

iniciate using a loop to create as many objects that you want of that type.

 for(var i=0;i<NumObjects;i++)
 {
 .
 .
 .

now make a Random posi to Instantiate t$$anonymous$$s.

 posi = Vector2(Random.value*area.x,Random.value*area.z) ;

and the y position find by a raycast from a very $$anonymous$$gh point,to not raycast down of the terrain, like t$$anonymous$$s:

         var $$anonymous$$t : RaycastHit;
    var yofsset : float;
         if (Physics.Raycast (Vector3(posi.x,500,posi.y), -Vector3.up, $$anonymous$$t)) {
         yofsset = $$anonymous$$t.distance;
         }


and the y value of the objects position is seted by 500 - yofsset...

 var realposi : Vector3 = Vector3(posi.x,500-yofsset,posi.y);
 Instantiate(wichobject,realposi,wichobject.transform.rotation);

now you have a random instanciator! note that can have merging objects, t$$anonymous$$s is only a test.

Comment
Add comment · Show 8 · 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 jaxsonp10 · Feb 18, 2014 at 12:26 AM 0
Share

What would all the code look like put together? I'm not sure what order to type all o this.

avatar image erick_weil · Feb 18, 2014 at 12:33 AM 0
Share
 #Pragma Strict
 var object : GameObject;
 function Start()
 {
 // the 100x100 is the area that become filled with your objects begining at origin
 generateisland(100,object,Vector3(100,0,100));
 }
 
 function generateisland(NumObjects : int,wichobject : GameObject,area : Vector3)
 {
 for(var i=0;i<NumObjects;i++)
 {
 posi = Vector2(Random.value*area.x,Random.value*area.z) ;
        var hit : RaycastHit;
    var yofsset : float;
        if (Physics.Raycast (Vector3(posi.x,500,posi.y), -Vector3.up, hit)) {
        yofsset = hit.distance;
        }
     
     var realposi : Vector3 = Vector3(posi.x,500-yofsset,posi.y);
 Instantiate(wichobject,realposi,wichobject.transform.rotation);
 }
 }
avatar image jaxsonp10 · Feb 18, 2014 at 01:02 AM 0
Share

Am I to attach this script to an empty game object, or?

avatar image erick_weil · Feb 18, 2014 at 01:37 AM 0
Share

attach this to any gameObject, and before asking here, test by yourself, the unity will not uninstall if you made ones mistakes...

and you need to assign a object to instance, in the inspector, see the "object" variable

an image to you

alt text

see the highlight area, is there you need to click to select a prefab to random instantiate, and remind to add your terrain(or a plane) to your scene, filling the area, (default 0,0 - 100,100)

avatar image jaxsonp10 · Feb 18, 2014 at 01:44 AM 0
Share

I tested it and got errors when I used the empty game object, So I was trying to make sure that wasn't the problem...

Show more comments
avatar image
0

Answer by thornekey · Feb 18, 2014 at 01:43 AM

when i was trying to spawn a pillar at a certain height i did t$$anonymous$$s:

 public double MaxHeight = 3.4f;
 public double MinHeight = -6.4f;
 
 void Spawn(){
     
       float rightScreenBound = 18;        
        float randomY = Random.Range(MinHeight, MaxHeight);
      Instantiate(PillarObject, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
 
     }

the variables up the top are positions :)

hope its relevant

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
avatar image
0

Answer by Nidre · Feb 18, 2014 at 01:36 PM

You can take a look the script a have wrote a w$$anonymous$$le ago. It creates objects in a defined area. You can define density, shape etc..

http://wiki.unity3d.com/index.php/PopulateField

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Tree position on random Terrain (y axis) 0 Answers

Best way to render a list of rooms 0 Answers

Generating random paths 1 Answer

More True Randomess - RNGCryptoServiceProvider explanation required 1 Answer

Placing trees randomly across a large terrain 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