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 Captaion · Jun 12, 2020 at 07:27 PM · gameobjectinstantiategenerationcoverspacing

Generate objects around object based on bounding size

Basically I'm trying to evenly generate and space objects around the "cover" object based on its bounding size and I've got it for whole numbers but I can't quite figure out why I can't get it to work for a size with a decimal point.


Any advice would be really appreciated, and I might be overlooking something obvious but I feel like I'm stuck, for the code below I used a standard 1x1x1 cube in the teeest variable, and the object you place the component on should be a standard cube 2x1x2 or bigger

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Cover : MonoBehaviour
 {
     public GameObject teeest;
     public Collider coverCollider;
     public MeshRenderer rend;
     public List<CoverNode> nodes;
     public List<GameObject> testList;
     public int numberOfNodes, nodesX, nodesZ, iterator, nodesInSet;
     public float spacingX, spacingZ, currentSpacing, distance, picketWidth;
     public string coverHeight, coverType;
     public Vector3 nodePos, oppositePos;
 
     private void Start()
     {
         coverCollider = GetComponent<Collider>();
         rend = GetComponent<MeshRenderer>();
 
         GenerateNodes();
     }
 
     public void GenerateNodes()
     {
         //This controls the switch below
         int mode = 0;
 
         //Round down the bounds of the renderer to get the nodes per side
         nodesX = Mathf.FloorToInt(rend.bounds.size.x);
         nodesZ = Mathf.FloorToInt(rend.bounds.size.z);
 
         //Spacing Calculation
         spacingX = (rend.bounds.size.x - (picketWidth * nodesX)) / (nodesX * 0.5f);
         spacingZ = (rend.bounds.size.z - (picketWidth * nodesZ)) / (nodesZ * 0.5f);
 
         //Number of nodes is the total generated
         numberOfNodes = ((nodesX + nodesZ) * 2);
 
         //Iterator is half of the total number as we do two sides at once
         iterator = numberOfNodes / 2;
 
         for (int i = 0; i < iterator; i++)
         {
             switch (mode)
             {
                 //x
                 case 0:
                     if (currentSpacing == 0)
                     {
                         currentSpacing = spacingX;
                     }
                     else
                     {
                         currentSpacing += spacingX;
                     }
 
                     distance = rend.bounds.extents.z + picketWidth;
                     nodePos = rend.bounds.center + (transform.forward * distance) + (transform.right * (distance - currentSpacing));
                     oppositePos = rend.bounds.center + (-transform.forward * distance) + (transform.right * (distance - currentSpacing));
 
                     nodesInSet++;
 
                     if (nodesInSet == nodesX)
                     {
                         mode = 1;
                         currentSpacing = 0;
                         nodesInSet = 0;
                     }
 
                     break;
 
                 //z
                 case 1:
 
                     if (currentSpacing == 0)
                     {
                         currentSpacing = spacingZ;
                     }
                     else
                     {
                         currentSpacing += spacingZ;
                     }
 
                     distance = rend.bounds.extents.x + picketWidth;
                     nodePos = rend.bounds.center + (transform.right * distance) + (transform.forward * (distance - currentSpacing));
                     oppositePos = rend.bounds.center + (-transform.right * distance) + (transform.forward * (distance - currentSpacing));
 
                     nodesInSet++;
 
                     if (nodesInSet == nodesZ)
                     {
                         currentSpacing = 0;
                         nodesInSet = 0;
                     }
 
                     break;
             }
 
             GameObject node1 = Instantiate(teeest, nodePos, transform.rotation);
             GameObject node2 = Instantiate(teeest, oppositePos, transform.rotation);
 
             testList.Add(node1);
             testList.Add(node2);
 
         }
     }
 }
 


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

0 Replies

· Add your reply
  • Sort: 

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

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

MonoBehavior.Start() called infinite times? 1 Answer

Set the same value to a new Game object as the old one? 2 Answers

How to be notified of new GamObject creation? 1 Answer

Instantiate GameObject on local axis rather than world axis 1 Answer

Unwanted 2nd empty gameobject 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