• 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 /
  • Help Room /
avatar image
0
Question by Bogomip · Mar 30, 2020 at 09:42 AM · positionscalepositioning

Organising children in the local position is out of place.

So I am trying to make my game automatically organise children into a pattern within a sprite, so my structure is something like:

Sprite

  • child sprite

  • child sprite

etc, and this is the code I am using to do this:

 public void organiseChildren()
 {
     float xPos = -spriteRenderer.bounds.size.x / 2f;
     float yPos = -spriteRenderer.bounds.size.y / 2f;
     float spacing = 0.5f;

     foreach(Transform child in transform)
     {
         child.localPosition = new Vector3(0 + xPos, 0 + yPos, -1f);

         if((xPos + spacing) > spriteRenderer.bounds.size.x/2f)
         {
             xPos = -spriteRenderer.bounds.size.x / 2;
             yPos += spacing;
         }
         else
         {
             xPos += spacing;
         }
     }
 }

However when it places the children they are all scaled out of where they need to be. Im sure this is an issue of scale but am unsure of how to remedy it.

If anybody has any suggestions, I would be very grateful :)

Thanks!

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
0

Answer by Bogomip · Mar 30, 2020 at 12:08 PM

Ahh, thanks! Its scaling it relative to the parent thats my issue - ok, thanks so much. I made those corrections and it was a step in the right directions :)

I should be able to move this forward myself from here, but thanks very much for the help :)

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 streeetwalker · Mar 30, 2020 at 11:05 AM

@Bogomip, Correct me so can understand what you are doing: the code is supposed to align rows of children within the rectangle defined by renderbounds starting at left and bottom of the renderbounds and working your way to the right and up ( assuming you are looking down on the scene and +y is up and +x is to the right).

It looks like it should work fine. If the bounds rectangle is defined by the parent, I believe the only thing that could affect your positioning is any scaling you've applied to the parent object. If so, all the positions will also need be scaled by the inverse of those respective scale amounts.

For example, assume you define the parent localScale (that's the only kind there is) on x = 0.5f, , the when you localPosition the child at x = -10f, it's actual localPosition relative to the parent will be x = -5f, because is scaled down by 0.5f.

To compensate for parent scaling, you will need to multiply all values by the inverse of the scale - same as dividing by the scale. It doesn't matter if the parent is scaled lager or smaller.

   // make a shortcut, probably outside any function
   private Vector3 scales = transform.localScale; 

    // then in your for each loop
   child.localPosition = new Vector3(  xPos / scales.x,  yPos / scales.y, -1f / scales.z ); 

   // note, if all parent axes are scaled by the same amount, you can just pick one and:
   child.localPosition = new Vector3( xPos,  yPos, -1f) / scales.y;

    // P.S. no need to state '0 + xPos', and so on, unless you plan on... 
    // substituting  zero with some value later 
    //   Also, Bounds.extents are already = Bounds.size / 2 in each dimension, 
    //   so you can skip the processing and just state:
     float xPos = -spriteRenderer.bounds.extents.x;



If you have not scaled the parent, then I think there must be something else going on.

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.

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

223 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 avatar image avatar image avatar image avatar image

Related Questions

Can you help explain this positioning conundrum to me! 1 Answer

Player Prefs not setting the position at start of the game 0 Answers

OnTriggerEnter and position problem 0 Answers

Unity2D trouble positioning object in script 1 Answer

Enemy's set position is incorrect? 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