- Home /
Grow Sprite image size from Edges instead of Center.
Hi, I am working on 2D game, in which I have multiples sprite Images and I want to increase the Height of Sprite Image on runtime and I can increase its size but its increasing from center so its also change its origin. I am doing like this "transform.localscale". Is this possible that I can just increase its height instead of increasing from center?
Answer by robertbu · Jan 08, 2014 at 02:03 PM
If you select the texture you are using in your sprite, you sill see a 'Pivot' setting. Change this setting 'Center' to 'Bottom'.
Thanks @robertbu, I changed Center to Bottom but getting same effect as getting on Center, I also changed to Bottom Right and Bottom Left these hold origin but it grow one side, I think I want Bottom as you suggest who can grow on both sided and hold its position, but I this not work. You can see in Inspector setting as attached image show. Should I need to do more?
I may be confused about how you have things structured. To make this work, the natural orientation of the spring needs to be vertical. That is, when the object has rotation (0,0,0), then the image needs to be vertical. How do you have your spring image oriented?
Thanks @robertbu, and sorry for wrong orientation. First I don't need to place my spring vertically I need to show my both spring as attached image show. What I want when red ball move(using finger moving) then spring also streched or squeze according to movement with smooth movement and when user release red ball then it come back to its original position. Same Like as in this game "Ball Popper". This may be easy but I am new in unity so $$anonymous$$indly suggest me better approach to accomplish this task.
The followup comment to what you want to do indicates that this problem involves a number of technical issues. Since UA question should focus on a single, specific issue, I'm going to answer the specific question you asked. The drag and drop, getting the spring to return to original position, calculating trajectory of the launched object, etc. are separate issues.
For what you are doing, I'd go back to placing the anchor in the middle of the sprite. You will need a separate sprite for each spring, and the image with rotation (0,0,0) must be vertical. Here is a bit of code that you can place on a spring to get it to scale between two positions:
#pragma strict
var goStart : Transform;
var goEnd : Transform;
var factor : float = 1.0;
function Start() {
SetPos(goStart.position, goEnd.position);
}
function Update () {
SetPos(goStart.position, goEnd.position);
}
function SetPos(start : Vector3, end : Vector3) {
var dir = end - start;
var mid = (dir) / 2.0 + start;
transform.position = mid;
transform.rotation = Quaternion.FromToRotation(Vector3.up, dir);
transform.localScale.y = dir.magnitude * factor;
}
This code goes on each spring. The SetPos() function takes two positions. You can derive these positions any way you want. In the rest of the script, I use two Transforms, but you could hard code a vector3 position for the anchor end of the spring. The 'factor' variable will depend on scaling factor of your sprite and may need to be adjusted based on your sprite's 'Pixels to Units' setting. I encourage you to explore this short script in a clean scene with two game object for start and end and a sprite. You can move the end points in the Inspector to test how it stretches the spring.
Your answer
Follow this Question
Related Questions
Scaled sprite distorts on rotation in different resolutions 0 Answers
How expensive is scaling in 2D unity? Is it better to pre-scale? 1 Answer
How do I scale a 2D sprite over time? 2 Answers
Script to flip a sprite over the x axis not working? C#, Unity2D 1 Answer
How To Sync Scale in UNET 2 Answers