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 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 things like trees/rocks in a certain part of the island, but still at random spots with a random number of things. If you do submit a script, I would like you to please explain what everything means and what everything does so I can change things if need be, If that wouldn't be that hard for you guys? I haven't messed with things like this 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 this.

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

and the y position find by a raycast from a very high point,to not raycast down of the terrain, like this:

         var hit : RaycastHit;
    var yofsset : float;
         if (Physics.Raycast (Vector3(posi.x,500,posi.y), -Vector3.up, hit)) {
         yofsset = hit.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, this 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 re$$anonymous$$d 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 this:

 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 while 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.

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

Why my area object is bigger than my object ? 1 Answer

Help with Generating Random Tiles 1 Answer

Random Button / Plane Generation. 0 Answers

How to generate random terrain for a platformer 0 Answers

Random number different from previous generated. 6 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