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 Mystique5 · Jun 21, 2017 at 10:42 AM · 2d gameunity5graphicsscalesprites

unity 2d: scaling sprites problem.

alt text

Above is my game. Dark blue rectangle is a room - it is made up of one empty game object as a parent and 4 children sprites that are separate lines with their own 2d box colliders.

I want my room to shrink either vertically or horizontally or both when I play the game as time passes by. The problem is that when I shrink the room the parent seen in the hierarchy above, the lines that don't "shorten" become squashed and look practically invisible. I want all sides to retain their thickness. alt text

When I shrink the two lines seperately without scaling the parent. The thickness is retained but the two other lines do not shift up and join the shortened lines and it no longer looks like a box.

Script I used to shrink the room:

 public class shrinkScript : MonoBehaviour {
     public const float timer = 10f; //fixed value for how long it must be counting down. helps reset timer.
     public float countDown; //time that changes
     public float shrinkRateY; //speed at which it shrinks (vertical length of room)
 
     float RoomY;
 
     bool gameOver;
 
     // Use this for initialization
     void Awake() {
          RoomY = transform.localScale.y;
 
         countDown = timer;
 
         shrinkRateY = (RoomY / countDown) * Time.deltaTime;
     }
 
     // Update is called once per frame
     void Update () {
         if (countDown > 0) {
             countDown -= Time.deltaTime;
 
             float scaleY;  //stored the scale of the gameObject's x  co-ordinate in a variable
             scaleY = Mathf.Clamp(RoomY, 0.05f, 0.9943201f);
             transform.localScale -= new Vector3(0, shrinkRateY,0);
         }
     }
 }

Is there any possible way to achieve this?

PS: I want the room to shrink in-game with a script attached and allow the two other sides whose scale isn't being changed to move up.

Many thanks,

screenshot-90.png (5.5 kB)
screenshot-88.png (1.6 kB)
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
1
Best Answer

Answer by FlaSh-G · Jun 21, 2017 at 11:42 AM

I'd just antiproportionally scale the colliders below your transform.localScale = line.

At the top:

 public Transform top;
 public Transform bottom;
 public Transform left;
 public Transform right;

And below your current line 26, with scale being transform.localScale:

 SetVerticalScale(top, scale.y);
 SetVerticalScale(bottom, scale.y);
 SetHorizontalScale(left, scale.x);
 SetHorizontalScale(right, scale.x);

SetVerticalScale and SetHorizontalScale are defined like this:

 public void SetVerticalScale(Transform t, float parentScale)
 {
   var scale = t.localScale;
   scale.y = 1f/parentScale;
   t.localScale = scale;
 }
 
 public void SetHorizontalScale(Transform t, float parentScale)
 {
   var scale = t.localScale;
   scale.x = 1f/parentScale;
   t.localScale = scale;
 }
Comment
Add comment · Show 4 · 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 Mystique5 · Jun 21, 2017 at 01:25 PM 0
Share

Thank you so much. It worked like a charm :)

Could you please expain what this line of code does:

scale.y = 1f/parentScale;

Once again - thanks!

avatar image FlaSh-G · Jun 21, 2017 at 02:06 PM 0
Share

The scale of an object is its own scale times its parent's scale. This means that when you scale the parent from 1 to 0.5, to keep the absolute scale, you need to double that scale from 1 to 2. The value passed to the parentScale parameter is the scale that we want to counter. The scale of the child objects thus needs to be 1 / parentScale, and that's what this line calculates.

avatar image Mystique5 FlaSh-G · Jun 21, 2017 at 02:31 PM 0
Share

alt text Thank you for the explanation. Sorry to bother you again but.. when the room begins to scale down at a certain point the other side of the line is being pushed down and not exactly joining the other edges. It's really testing my OCD to the next level.

I do not know if it is the way I positioned the sprite or the code. (i tried moving each line and resizing but to no avail)

screenshot-91.png (1.2 kB)
avatar image FlaSh-G Mystique5 · Jun 21, 2017 at 03:27 PM 0
Share

I can suggest another solution then.

Unparent the five objects (the scaled room and the four walls) so they're not children of each other anymore, ins$$anonymous$$d sitting around next to each other in the hierarchy. Then, change this line

 scale.y = 1f/parentScale;

to this

 scale.y = parentScale;

in both functions. At last, replace the four function calls from earlier with this:

 top.localPosition = Vector3.up * scale.y / 2f;
 SetHorizontalScale(top, scale.x);
 bottom.localPosition = Vector3.down * scale.y / 2f;
 SetHorizontalScale(bottom, scale.x);

 left.localPosition = Vector3.left * scale.x / 2f;
 SetVerticalScale(left, scale.y);
 right.localPosition = Vector3.right * scale.x / 2f;
 SetVerticalScale(right, scale.y);

This will move and scale all four walls with values taken from the room's scale, but there's no additional scaling or moving from the scale because of parenting.

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

79 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

Related Questions

What do you put in the Project Settings>Graphics>Transparency Sort Axis, and what does those values mean (e.g. the difference between x = 1 and x = 69) ? 0 Answers

Blacked Out Images on build 0 Answers

How do I get the crisp sprite look? 2 Answers

2D graphic problem with tilemap and resolution 0 Answers

Spikes for a platformer game 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