Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Sealing gap created when scaling 2D square

Discussion in '2D' started by Lamprey, Jun 6, 2021.

  1. Lamprey

    Lamprey

    Joined:
    May 26, 2015
    Posts:
    36
    Hello, I have a problem with achieving perfect non-uniform scaling. Here is an example of the issue:

    scaling_problem.png

    How do I mathematically adjust the scaling of the sprites to seal the square?
    I've been trying different approaches, but nothing has worked.

    Code (CSharp):
    1. //Upper Section
    2. tileSection.tileTransform.localPosition = new Vector2(0.5f * transform.localScale.x, 0);
    3. tileSection.tileTransform.localScale = new Vector3(transform.localScale.x, 1 / transform.localScale.y, 1 / transform.localScale.z);
    Thank you!
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi.

    So are you trying to create something like a nine slice sprite?

    Can't you use UI system and let it to do this for you?
     
  3. Lamprey

    Lamprey

    Joined:
    May 26, 2015
    Posts:
    36
    This isn't a UI component, it's a set of eight sprites with box colliders.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    OK, I simply asked if you could use UI for whatever you are trying to do.

    If you are looking for a way to scale something to fit some distance between a and b you could do something like this:
    Code (CSharp):
    1. blockScale = distance / blockLen;
    So if your block has length of 1.5 and your distance between a and b is 3, then the resulting blockScale will be 2.