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 Robs · Feb 18, 2014 at 08:21 AM · scale

How do I dynamically scale and move groups of objects without them overlapping?

I am making a 2D endless runner and I have coins that are created at random sizes and recycled as the player progresses through the world. I apply a random scale value to the coin sprite and then add another directly next to it.

My code works well when I use it on a single prefab coin but I want to expand its functionality to work with a prefab that uses an empty gameObject with coins as children so I could create various patterns of coins for the player to pick up.

The problem that I am running into is that the prefabs overlap each other when I apply different scales to each and try to put them next to each other dynamically. I think the issue is with how I am trying to find the size of the objects using encapsulate, but I'm not sure.

Could someone please take a look at my recycle code and let me know if they can see anything obvious that I am doing wrong?

     private void Recycle(){
         Transform temp = objectQueue.Dequeue ();
         float adjustedDistance = 0.0f;
         oldBounds = totalBounds;
 
         if (useRandomGapSize) {
             gapSize = Random.Range (minGap, maxGap);
         }
 
         if (useRandomHeight) {
             float height = 0;
             height = Random.Range (minHeight, maxHeight);
             nextPosition.y = height;
         }
 
         if (useRandomScale) {
             scale = new Vector2 (Random.Range (minScale.x, maxScale.x), Random.Range(minScale.y, maxScale.y));
 
         }
 
         temp.localScale = scale;
 
           if (isGroup) {
             Transform[] groupOfObjects;
             int childCount = temp.childCount;
             groupOfObjects = new Transform[childCount];
                 totalBounds = new Bounds(temp.position, Vector3.zero);
             for (int i = 0; i < childCount; i++){
                 groupOfObjects[i] = temp.GetChild(i);
                 totalBounds.Encapsulate(groupOfObjects[i].renderer.bounds);
             }
             adjustedDistance = totalBounds.size.x;
 
         } else {
             sprite = temp.GetComponent<SpriteRenderer> ();
             spriteSize = sprite.bounds.size;
             Vector2 oldSpriteSize = spriteSize; 
             spriteSize.x = sprite.bounds.size.x * temp.localScale.x;
             spriteSize.y = sprite.bounds.size.y * temp.localScale.y;
             adjustedDistance = (spriteSize.x + oldSpriteSize.x) * 0.5f;
         }
         nextPosition.x += adjustedDistance + gapSize;
         temp.localPosition = nextPosition;
         objectQueue.Enqueue (temp);
     }

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
0

Answer by Owen-Reynolds · Feb 18, 2014 at 04:34 PM

I'm not seeing the purpose of all that code, but ...

Seems, with coins all children of an empty parent, you could just scale the parent. All sizes and distances (object centers) should scale perfectly together.

Only issue would be with rotated non-uniform scaling problem (x*2, y*1, z*3.) Might need to always scale all axii the same.

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 Robs · Feb 18, 2014 at 10:29 PM 0
Share

The parent is being scaled but, I thought that in order to know where to place the next set of coins, I would need to figure out the bounds of all of the coins inside of the empty group. Are you saying that I don't need to do that?

avatar image Owen-Reynolds · Feb 19, 2014 at 07:16 AM 0
Share

If you know the original bounds, of the "coin set," scaling the parent should linearly scale the bounds. Say you have a sphere trigger. It should always scale to perfectly fit, as the parent does (but spheres really, really, want x,y&z all scaled the same.)

I mean, sure, gathering and combining bounds by hand is fun, and a shame to let Unity have the pleasure of doing it automatically, but there's plenty of stuff to program that Unity won't do for you :-)

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

19 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

Related Questions

GameObject Scale as World Coordinates (Units)? 1 Answer

How do I permanently resize a model with children? 2 Answers

How to export .OBJ from Editor with rescaled mesh? 1 Answer

[solved] Get upward orientation of object and add scale value 1 Answer

scalefactor issue 0 Answers


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