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
3
Question by wolga2 · Jul 04, 2012 at 10:37 AM · spread

Spread Gameobjects on Terrain

is it possible to write a code that spreads a number (for example 5) of a gameobject (for example the enemy) within a specific room on the terrain when pressing play? the gameobjects should be spread by coincidence and therefore on different places everytime i press play. thank you!!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by wolga2 · Jul 04, 2012 at 06:10 PM

the code works perfectly, thank you so much!! my problem now is, that the instantiated object is a navmeshagent and it will be placed on non-navmesh-places as well. is there a solution to avoid this? thanks!

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
-1

Answer by wolga2 · Jul 04, 2012 at 03:25 PM

the code works perfectly, thank you so much!! but my problem is now, that the object is a navmeshagent and it will put the instantiated object on non-navmesh-places as well. what can i do about that? thanks!

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 khellstr · Jul 04, 2012 at 11:13 AM

http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

Should get started with this:

 var prefab : Transform;
 
 for (var i : int = 0;i < 5; i++) {
     Instantiate (prefab, Vector3(Random.value, Random.value, Random.value), Quaternion.identity);
 }
Comment
Add comment · Show 7 · 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 Luci85 · Jul 04, 2012 at 11:18 AM 0
Share

Of course, you have to set certain borders for the random value (not that the enemy spawns somewhere outside the room, or floats in the air above it) ;-)

avatar image khellstr · Jul 04, 2012 at 11:38 AM 0
Share

Random.value returns something between 0.0 - 1.0, so probably it have to be multiplied, depending of scene size. Also should check if the place is free where game object spawn. and other stuff...

I'm pretty sure there is ready-made spawner script some where in internets. Recommend google.

avatar image AlucardJay · Jul 04, 2012 at 11:55 AM 0
Share

google, yes, but the question is here and the asker is most likely a new user. Here's a more concise answer showing the points in the above comments :

 #pragma strict
 
 public var instPrefab : Transform;
 public var instAmount : int = 5;
 public var instRange : Vector3 = Vector3( 5, 0, 2 ); // range of random position, represents -value to +value
 
 
 function Start() 
 {
     SpawnObjects();
 }
 
 function SpawnObjects() 
 {
     var instPosition : Vector3;
 
     for (var i : int = 0; i < instAmount; i++) // loop through until the amount is reached
     {
         instPosition = Vector3( Random.Range(-instRange.x, instRange.x), Random.Range(-instRange.y, instRange.y), Random.Range(-instRange.z, instRange.z) ); // create a random position, from -instRange to +instRange
  
         Instantiate (instPrefab, instPosition, Quaternion.identity); // Instantiate the object there
     }
 }
avatar image AlucardJay · Jul 04, 2012 at 11:56 AM 1
Share

Instantiate : http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

Random : http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

avatar image khellstr · Jul 04, 2012 at 12:37 PM 0
Share

Sorry about that google, did not mean to be patronizing.

For me it's often so that I know what I want to do but don't know the right words for searches. English is not my native language. So I though "spawn" could be key word for OP, he doesn't not mention it in question but is clearly what he needs to do. So probably I should have written try google "spawn script" etc.

Show more comments

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

8 People are following this question.

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

Related Questions

how would i add bullet spread? 0 Answers

Spread material over multiple objects or cubes 1 Answer

Maya pistol animation splits up when played 0 Answers

Gas/Liquid spreading in area 1 Answer

access DOF values via script 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