- Home /
unity 2d: scaling sprites problem.
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.
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,
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;
}
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!
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.
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)
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
Follow this Question
Related Questions
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