Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ratchetunity · May 31, 2019 at 08:15 AM · scalechild objectalgorithmlogicformula

Scaling gameobject using compound rule of three

Hi!

I'm trying to scale gameobjects depending on their childcount using a compound rule of three. So I have a max scale (0.25) for the gameobject with the highest childcount and a min scale (0.1) for the gameobject with the lowest one, and the values I should get must be between maxScale and minScale. Let's say that the highest childcount is 108 and the second highest is 13. The value I should get for 13 must be closer to 0.25 than to 0.1. I know I must be missing something. This is my code:

    public GameObject[] goArray;

 private void GetHighestChildCount ()
 {
     List<int> numberOfChildrenList = new List<int>();
     for (int i = 0; i < goArray.Length; i++)
     {
         numberOfChildrenList.Add(goArray[i].transform.childCount);
     }
     maxNumberOfChildren = Mathf.Max(numberOfChildrenList.ToArray());
 }

 private void ScaleGO ()
 {
     float maxScale = 0.0625f;
     float minScale = 0.01f;
     
     for (int i = 0; i < goArray.Length; i++)
     {
         float newScale = (maxScale * minScale * goArray[i].transform.childCount) / (maxNumberOfChildren * minScale);
         goArray[i].transform.localScale = new Vector3(newScale, newScale, newScale);
     }
 }
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
Best Answer

Answer by Bunny83 · May 31, 2019 at 09:07 AM

This line makes not too much sense:

 float newScale = (maxScale * minScale * goArray[i].transform.childCount) / (maxNumberOfChildren * minScale);

Just by reduction you can completely remove minScale from your calculations since you multiply by minScale on the "top" and you also divide by minScale on the "bottom". So this line would be similar to:

 float newScale = (maxScale * minScale * goArray[i].transform.childCount) / (maxNumberOfChildren * minScale);

What you actually want to do is this:

 float newScale = minScale + ((maxScale - minScale) * goArray[i].transform.childCount) / (maxNumberOfChildren);


Though another probably simpler solution is to use Lerp since that's just what Lerp does.

 float newScale = Mathf.Lerp(minScale, maxScale, (float)goArray[i].transform.childCount / maxNumberOfChildren);


Even your first approach would be easier to understand to break it down into it's parts. Specifically:

 float range = maxScale - minScale;
 float t = (float)goArray[i].transform.childCount / maxNumberOfChildren;
 float newScale = minScale + range * t;

The same could be done with my Lerp example to make it more readable:

 float t = (float)goArray[i].transform.childCount / maxNumberOfChildren;
 float newScale = Mathf.Lerp(minScale, maxScale, t);


Note that the float cast is important since both arguments (childCount and maxNumberOfChildren) are integers. If you divide two integers the result is an integer as well. Since this calculation will always be a fraction between 0 and 1 it would always trucate to 0 without the cast. If you declare the "maxNumberOfChildren" variable as float instead of int you would not need the cast.

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 ratchetunity · May 31, 2019 at 11:50 AM 0
Share

Thak you @Bunny83 ! It worked like a charm!

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

108 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

Related Questions

Mesh gets squashed after parenting to camera. 0 Answers

Check if all values in a list are equal 1 Answer

Cube Sorter 0 Answers

Rotated child's geometry changed when attach to parent 1 Answer

Generate tiles along a path a certain distance before reaching end 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