• 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 iHaveReturnd · Nov 28, 2012 at 07:52 PM · instantiatebounds

Weird problem: Spawning units inside a box

I'm making an asteroid belt generator. I'm trying to make it so that you set the size of the spawn space using a cube with no renderer.

Everything spawns and works other than the fact that they will only spawn around the edge of the cube.

I'm guessing I'm just not thinking of something in the math I did or there may be some nuance I'm not thinking of. Here is the code:

 var numberOfAsteroids : int;
 
 var asteroidPrefabs : GameObject[];
 
 function Awake(){
     CreateBelt();
 }
 
 function CreateBelt(){
     var sizex = collider.bounds.size.x;
     var sizey = collider.bounds.size.y;
     var sizez = collider.bounds.size.z;
 
     var currentPos = transform.position;
     
     for(i = 0; i <= numberOfAsteroids; ++i){
         var position = Vector3(Random.Range(currentPos.x - (sizex/2),currentPos.x + (sizex/2)),
                                  Random.Range(currentPos.y - (sizey/2),currentPos.y + (sizey/2)),
                                   Random.Range(currentPos.z - (sizez/2),currentPos.z + (sizez/2)));
 
         var newAsteroid = Instantiate(asteroidPrefabs[Random.Range(0,asteroidPrefabs.length)], position, transform.rotation);
         
     }
 }

I thought that should take the current position, find the min and max for each value, pick a random number. That for x,y,z. Then spawn the object, but there is a disconnect somewhere and I'd appreciate help figuring that out.

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

2 Replies

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

Answer by Michael Covert · Nov 28, 2012 at 08:07 PM

Just a first guess, but perhaps your collider is pushing them out via physics?

You could try disabling the collider to see if that's the case (leaving it available for sizing, but removing it from the physics stuff). Alternatively, try logging out your positions to double check that you're not just being hit by unlucky random values.

Comment
Add comment · Show 4 · 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 iHaveReturnd · Nov 28, 2012 at 08:12 PM 0
Share

Turning off the box collider causes them to all spawn in the middle, then they all either move on the x axis in the positive or negative direction.

I did check the values, I just removed the print statements from the code for the post. They just spawn along the edges of the "bounding box"

Are you sure the sizing is still available with it off? Edit: It says it counts as an empty bounding box with the collider disabled in the scripting reference. Hm...

avatar image iHaveReturnd · Nov 28, 2012 at 08:25 PM 0
Share

sizex and what not return 0 with the collider disabled.

If I set it to a trigger, the values are set fine. However nothing spawns. It gets to the instantiate and nothing appears to happen.

avatar image Michael Covert · Nov 28, 2012 at 08:26 PM 0
Share

Hmm; I thought you could, but I guess not - it looks like the bounds becomes 0,0,0 when it's disabled. $$anonymous$$y apologies for that. I guess I'd try using something else then - unity cubes are 1x1x1 by default, so you could just use the cube's scale for the bounds, assu$$anonymous$$g the collider matches the box size.

avatar image iHaveReturnd · Nov 28, 2012 at 08:33 PM 0
Share

Scale did the trick. So: var sizex = transform.localScale.x; var sizey = transform.localScale.y; var sizez = transform.localScale.z; And disabled the collider.

Thank you very much :)

avatar image
0

Answer by loopyllama · Nov 28, 2012 at 08:11 PM

perhaps try explicitly casting to a float? something like changing this:

 Vector3(Random.Range(currentPos.x - (sizex/2),currentPos.x + (sizex/2)),

to this:

 Vector3(Random.Range((float)(currentPos.x - (sizex/2)),(float)(currentPos.x + (sizex/2))),

also fwiw, Bounds.extents is half the size of the...well...half the size of the size

Comment
Add comment · Show 2 · 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 iHaveReturnd · Nov 28, 2012 at 08:18 PM 0
Share

I switched it to extents. Js doesn't like adding float in like that apparently. It's throwing constructor errors.

avatar image loopyllama · Nov 28, 2012 at 08:34 PM 0
Share

javascript casting is different. you can always change the 2 to 2.0f

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

12 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

Related Questions

Getting Collider2D.bounds.extents to update immediately (2D) 0 Answers

Instantiate GameObjects inside a collider 1 Answer

Set bounds of collider added at runtime 1 Answer

Array of prefabs with different sizes stick to each other 0 Answers

Instantiate Left-Edge of Prefab at Right-Most Edge of Another 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