Search code examples
unity-game-enginemathscale

How do I rescale my gameobject to match with original gameobject?


I have a grid of game objects in which I can change the number of game objects that fit inside the grid. The grid is a child of a game object. I wish to rescale all other grids to match my 4X4 grid size programmatically.

Desired size

Desired size

Grid to rescale

Grid to rescale

Each game object is 1 unit and the parent transform scale is (1,1,1). I know that if my grid is 8X8 that I would probably need to rescale to 0.5. I only have an issue in trying to calculate the rescale number.


Solution

  • So again as said it is pretty simple maths.

    You have a grid with side length 5, you want 4.

    So you normalize it via

    currentSize / 5.0f * 4.0f
    

    which would be a factor of 0.8f.

    Since you need to apply it to each side of your grid you share that so it has to be 0.8f * 0.8f which is a factor of 0.16f.


    Or if you say it is 8x8 then the same calculations

    currentSize / 8f * 4f
    

    so a factor of 0.5. then squared so 0.25f.