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. Dismiss Notice

Resize sprite when it is repositioned on screen.

Discussion in 'Scripting' started by Retro420, May 11, 2018.

  1. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    23
    Hello.
    I am making a jigsaw game, all is going well except one little problem.

    I have the "pieces" in a scrollable inventory. They are all quite small, but visable, as I don't want them taking the bulk of the screen. I want the pieces to scale up when they are placed in their correct position, as otherwise the finished puzzle would be tiny.

    I can't even think where to start with this. I am still a beginner, learning as I go, but this has me stumped!

    Any help or advice on at least a starting point would be great as this is really annoying me now.

    Thanks.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Well, once the piece is in place, I'm guessing you're doing something to check that it's in the right place. Just change the scale of it at the same time. You could do this has a hard set, but that might look strange. I would probably use a tween engine so it looks like it "grows" into place.
     
    Retro420 likes this.
  3. FantasticGlass

    FantasticGlass

    Joined:
    May 31, 2016
    Posts:
    38
    You could use a boolean to control whether or not it is in the correct position or not.
    And two Vector3 variables to control it's scale.
    And a Method to change it.

    Code (CSharp):
    1.  
    2. bool inPosition = false;
    3. Vector3 minScale = new Vector3(0.5f, 0.5f, 0.5f);
    4. Vector3 maxScale = new Vector3(2f, 2f, 2f);
    5.  
    6. void ControlScale()
    7. {
    8.     if (inPosition)
    9.     {
    10.         transform.localScale = maxScale;
    11.     }
    12.     else
    13.     {
    14.         transform.localScale = minScale;
    15.     }
    16. }
    17.  
    When the piece is placed properly you would then set inPosition to true.
    You could call this method in Update (), or just after the piece is placed.
     
    Last edited: May 12, 2018
    Retro420 likes this.
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This code:
    Code (csharp):
    1. Vector3 minScale = 0.5f, 0.5f, 0.5f;
    2. // should be:
    3. Vector3 minScale = new Vector3(0.5f, 0.5f, 0.5f);
    4. // same for maxScale
     
    Retro420 likes this.
  5. FantasticGlass

    FantasticGlass

    Joined:
    May 31, 2016
    Posts:
    38
    oops. my bad, fixed my post. thx
     
  6. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    23
    The worst thing about helpful people is that all these answers make perfect sense to me, but I couldn't think of them on my own lol.

    Thanks everyone for the help, will give it a go later.
    Thanks for the tween engine tip as well, grateful to everyone.
    Thanks.
     
    FantasticGlass likes this.
  7. FantasticGlass

    FantasticGlass

    Joined:
    May 31, 2016
    Posts:
    38
    With repetition you will become better and better.
     
    Retro420 likes this.