Search Unity

Resolved localScale /= issue

Discussion in 'Scripting' started by Snakebearer, May 29, 2022.

  1. Snakebearer

    Snakebearer

    Joined:
    Feb 9, 2013
    Posts:
    26
    Hey there good people.
    I'm in a bit of a bind, and I think I'm just misunderstanding how to apply a change to localScale.

    I think I messed up the math bad, but however I flip it and experiment with it, it just doesn't seem to work like I imagine it would.

    I use another script to instantiate gameobjects as a children to an empty gameobject.
    I'm trying to scale that empty gameobject with localscale depending on the amount of instantiated objects.
    This was simple enough. But controlling that has turned into a real bother.

    The graphics are larger and larger circles. All of them have the same scale as game objects, but the image files all have the same dimensions (1000x1000 pixels), with the circle having different sizes inside the actual image file. (i.e. the smallest is 200 pixels, all the way up to 1000.) So the more circles, the larger they become. Script for that is already done and finished.

    The goal is to resize the the empty gameobject depending on how many instantiated circles I spawn.


    Currently, I've set it up like this:

    Code (csharp):
    1.  
    2.     [SerializeField] private int objectAmount;
    3.     [SerializeField] private GameObject gameContainer;
    4.    // this is just to reference the parent object for the int setting the amount of gameobjects I want to spawn.
    5.  
    6.     void Start()
    7.     {
    8.        
    9.         gameContainer = this.transform.parent.gameObject;
    10.  
    11.  
    12.         objectAmount = gameContainer.GetComponent<gamescript>().amountToSpawn;
    13.         transform.localScale /= (objectAmount * (1.1f / objectAmount));
    14.    //here is the issue. I think I've misunderstood how the localScale function works.
    15.  

    I mean, I'm very sure I'm messing up the math, and the current example is just.. Well.. The current attempt I'm at to understand the localScale /= function. If anyone can enlighten me, I'd be very grateful.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,794
    Uhhh, this

    (objectAmount * (1.1f / objectAmount));

    Boils down to 1.1.

    objectAmount does nothing.
     
    Bunny83 and Ryiah like this.
  3. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    localScale /= a;
    is just shorthand for
    localScale = localScale / a;
    .

    Vector3's / operator is just shorthand for:
    Code (CSharp):
    1. localScale.x = localScale.x / a;
    2. localScale.y = localScale.y / a;
    3. localScale.z = localScale.z / a;
    When you divide a number by
    1.1
    the number gets smaller.
    As I understand it what you're after is making the numbers bigger.
    So you want to multiply the number instead:
    Code (CSharp):
    1. float scale = objectAmount * 1.1f;
    2. localScale = new Vector3(scale, scale, scale);
    So now as objectAmount increases so does the scale:
    1 * 1.1 = 1.1
    2 * 1.1 = 2.2
    3 * 1.1. = 3.3
    ...
     
    Bunny83 likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,004
    So I guess you want to scale down the object depending on how many objects you have?. First of all your code is in start, so this is only called once. The next question is now much do you want to scale it down for each object you have?

    Note that working with percentage multiplies is tricky and so many people get it wrong. Note that scaling something down by 10% and scaling something up by 10% are not opposite operations if you do a relative scaling. When you scale something up by multiplying it by 1.1, the reverse is not multiplying it by 0.9, but by 0.909090909... (which is 1/1.1)- So it seems strange that you use 1.1 and divide by it as it would essentially indicate a shrinking factor of about 9%

    Anyways, if you want to scale down the object by anout 10% for each object you have, you have to use Mathf.Pow like this

    Code (CSharp):
    1. transform.localScale *= Mathf.Pow(0.9f, objectAmount);
    This gives you a downscaling factor like this:

    Code (CSharp):
    1. 1    0.9
    2. 2    0.81
    3. 3    0.729
    4. 4    0.6561
    5. 5    0.5905
    6. 6    0.5314
    7. 7    0.4783
    8. 8    0.4305
    9. 9    0.3874
    10. 10   0.3487
    This is the overall zooming factor by 10% for each object you have. So when you have 10 objects you have zoomed out about 3 times (x2.868 == 1/0.3487). That way you get a linear zooming effect. Though it's not clear if that's what you actually want since it scales linearly with the number of objects. Circles are 2d objects and they occupy an area. So if the number of objects increase linearly (if each object occupy about the same area), the area would go up by the square root of the number of elements.

    So it's difficult to say what's the right solution in your case as your description what's actually happen is quite abstract.
     
    SisusCo likes this.
  5. Snakebearer

    Snakebearer

    Joined:
    Feb 9, 2013
    Posts:
    26
    Okay, that clears up a few things. I think I'm closer to a solution, but I'm not completely there yet.
    I do think I've found a solution I'm kind of happy with. But since it's not Exactly like I want it, I might ruin it for myself later down the line.

    The answer was to use Mathf.Pow
    The issue was that 0.9f wasn't actually what I needed to do this. The conversion between the actual size of the image and the scale it has in the gameobject translated weirdly.
    If I used 0.9f, it got into the right ball park, the end result made each object just a slight fraction larger than the last.
    While 0.8 made each object slightly smaller. So there is probably an exact sweet spot somewhere between there.
    I'm going to stick with 0.875f for the moment. It still makes each object slightly larger than the last - but it won't affect the gameplay that much since it still stays aesthetically within the background graphics! :)

    Thank you so much for helping me out, even though it seems my old school math isn't up to match anymore. XD