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 DevMerlin · Feb 22, 2013 at 07:12 AM · instantiatecolliderprefab

How can I make this spawning system work?

I have a custom spawning system that takes defined space from each tile in my random map generator, selects them and places objects in those spaces accordingly. However, at the moment the system is far from accurate. I'm having a lot of trouble getting objects to place properly (and the parent tile is 1x1x1 scale) and I think it has something to do with my calculations. I got it to work partially using a / 2 division but that just says something is really off. The three main pieces of code are:

 void populateArea(GameObject obj)
 {
     string areaType = obj.tag;
     
     float areaX = obj.collider.bounds.size.x;
     float areaZ = obj.collider.bounds.size.z;
     
     float posX = obj.transform.position.x;
     float posY = obj.transform.position.y;
     float posZ = obj.transform.position.z;
     
     Vector3 objLocation = new Vector3(posX, posY, posZ);
     
     switch(areaType)
     {
         case "Sides":
             placeGrass(objLocation, areaX, areaZ);
         break;
         case "Over":
 
             placeLogs(objLocation, areaX, areaZ);
             
         break;
         
         case "Under":
             placeRocks(objLocation, areaX, areaZ);
         break;
     }
 }
 
 Vector3 placeInsideConstrains(Vector3 orgVector, float areaX, float areaZ)
 {
     Vector3 placeObjec = new Vector3(Random.Range(-areaX,areaX), 0, Random.Range(-areaZ,areaZ));
         
     Vector3 returnVector = orgVector + placeObjec;    
     
     return returnVector;
 }
 
 void placeLogs(Vector3 originalVector, float areaX, float areaZ)
 {
     Transform logObj = (Transform)Instantiate(Landscaping[1]);
     
     int randomRotationY = Random.Range (0,360);
     
     logObj.transform.Rotate(new Vector3(0,randomRotationY,0));
     
     logObj.transform.parent = rezzedLand.transform;
     
     logObj.transform.position = placeInsideConstrains(originalVector, areaX, areaZ);
 }

placeLogs() is an example of how objects are placed with the system. All of the variables come from the bound gameobjects, being defined by colliders. Over, Under, and Sides are the possible positions within the spawning zones, used to define which objects can be placed where.

Comment
Add comment · Show 3
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 Fattie · Feb 22, 2013 at 07:52 AM 0
Share

possibly this could help

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

cheers

avatar image DevMerlin · Feb 22, 2013 at 07:58 AM 0
Share

To be honest I'm not sure if that does apply. They seem to be asking how to keep track of objects. I'm trying to place objects within a specific area defined by boundaries.

avatar image GC1983 · Feb 22, 2013 at 09:11 AM 0
Share

Your float variables assigned to transform.position.x,y,z are pointless. Just assign them as their modular float variables as posX, posY, posZ and place them in your Vector3 arguments. Youre kinda reassigning each transform.position when you assign all of the variables into the new Vector3().

You could also use multiple collider objects that trigger different responses ins$$anonymous$$d of trying to code random events from certain sides of the collider.

1 Reply

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

Answer by Ellandar · Feb 22, 2013 at 09:32 AM

G'day Teonnyn,

Just thinking that it could be your definitions of areaX and areaZ at the start. collider.bounds.size.x and y both return the full X and Z values.

I believe what you may be after is collider.bounds.extents.x and z

  • Size = full width height

  • extents = distance from center of x to edge, and similar for Z.

That probably explains why using /2 gets you closer.

Rob.

Source: http://docs.unity3d.com/Documentation/ScriptReference/Bounds.html

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

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

How to instantiating object whith collider not colliding. 1 Answer

Error[Component CircleCollider2D could not be loaded when loading game object] 0 Answers

Help On Checking OverLaping in instantiating 0 Answers

Updating a variable on a script in an instanced object 1 Answer

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 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