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 hakermania · Jul 15, 2013 at 07:24 PM · arraycube

Cannot Instantiate to the right position

I have a panel (resized box) and I try to create an 8x4 array of other planes inside of it. I am trying to calculate how to position the whole array at the center of the panel but to no avail.

This is what I've tried so far (elementPrefab is the small plane, while the script is attached to the panel):

 //the space on the edges of the panel, where the array shouldn't be
 var x_extra_space : float = (transform.localScale.x-8*elementPrefab.transform.localScale.x)/2.0;
 var y_extra_space : float = (transform.localScale.y-4*elementPrefab.transform.localScale.y)/2.0;
 //creating 32 elements and positioning them
 for( var i : int = 0; i < 32; i++){
     var curElement : GameObject = Instantiate(elementPrefab,
         Vector3( x_extra_space + (2*(i % 8)+1)*(elementPrefab.transform.localScale.x/2.0) - transform.localScale.x/2.0,
             -y_extra_space-(2*(i / 8)+1)*(elementPrefab.transform.localScale.y/2.0) + transform.localScale.y,
             transform.position.z - elementPrefab.transform.localScale.z),
             Quaternion.identity);

     curElement.transform.parent=transform; 
 }

From my experience with what I'm trying to do, if the position of a child is at (0, 0), then it is right at the center of the parent.

That's an explanation for better understanding:

preview

This is what I get when I execute the above code (the gray area is the panel, while the planes are clearly visible):

preview

What's wrong with my code? As I think it, it should work.

image 1.png (11.8 kB)
image1.png (8.1 kB)
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

1 Reply

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

Answer by robertbu · Jul 15, 2013 at 10:33 PM

I found what you were doing to be somewhat convoluted. In addition, since you are not sizing the 'elementPrefab' game objects, you should not care about the extra space. That is, you want to center your 8 x 4 grid on the parent game object, and the extra space will be any space left over. Likewise, your calculations need not use the scale of the parent object. You use 'transform.localScale' on lines 7 - 9, which is likely the root of your issues. Here is a quick rewrite:

 var elementPrefab : GameObject;
 
 var cols = 8;
 var rows = 4;
 
 function Start() {
     var width  = elementPrefab.transform.localScale.x;
     var height = elementPrefab.transform.localScale.y;
     
     var v3StartPos : Vector3;
     v3StartPos.x = transform.position.x - cols*width / 2.0  + width / 2.0;
     v3StartPos.y = transform.position.y - rows*height / 2.0 + height / 2.0;
     v3StartPos.z = -0.1;
     
     for( var i : int = 0; i < cols; i++){
         for (var j = 0; j < rows; j++) {
             var curElement : GameObject = Instantiate(elementPrefab);
             curElement.transform.position = Vector3(i * width, j * height, 0.0) + v3StartPos;
             curElement.transform.parent=transform; 
         }
     }
 }
Comment
Add comment · Show 1 · 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 hakermania · Jul 16, 2013 at 07:46 AM 0
Share

Hello, your solution worked just fine :) The only thing that I had to alter was the local Z (set it to -0.1). This is my final code:

 var width  = elementPrefab.transform.localScale.x;
     var height = elementPrefab.transform.localScale.y;
  
     var v3StartPos : Vector3;
     v3StartPos.x = transform.position.x - cols*width / 2.0  + width / 2.0;
     v3StartPos.y = transform.position.y - rows*height / 2.0 + height / 2.0;
     v3StartPos.z = -0.1;
  
     for( var i : int = 0; i < cols; i++){
        for (var j : int = 0; j < rows; j++) {
             var curElement : GameObject = Instantiate(elementPrefab);
             curElement.transform.parent=transform; 
             curElement.transform.position = Vector3(i * width, j * height, 0.0) + v3StartPos;
             curElement.transform.localPosition.z=-0.1;
        }
     }

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

15 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

Related Questions

Creating a grid of cubes. 1 Answer

Move one vertice of my cube 1 Answer

Grid matching issue 0 Answers

Cycle Through Array - Change Values 0 Answers

Detection if a GameObject is below you or next to you? 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