- Home /
Scale Animation
Hi, I'm wondering how I could do a scale animation. I would like to do something similar to what we can do with Lerp. With this method, we can do an animation in which, e.g., the object starts at a position1 and ends at a position2, traversing this path at a constant speed and in a certain time.
I would like to do something like this, but with the size. I have a GUITexture that starts with an specific size1 and I would like that it ends with a size2. So, I would like to be able to simulate the GUITexture getting close to the screen, or getting away from the screen (without change the camera position).
So far, I have this, but I don't have much idea about how I could do that...
Texture animationTexture;
GUITexture animationGUI;
GameObject animationGO;
void Start ()
{
animationGO = new GameObject();
animationGO.transform.position = Vector3.zero;
animationGO.transform.localScale = Vector3.zero;
animationGUI = animationGO.AddComponent<GUITexture>();
animationGUI.name = "Animation";
}
//Just to set the animation texture and its properties.
void setAnimationTexture (string animationResourcePath, Vector2 position, Vector2 size)
{
animationTexture = Resources.Load(animationResourcePath) as Texture;
animationGUI.enabled = true;
animationGUI.texture = animationTexture;
animationGUI.pixelInset = new Rect(position.x, position.y, size.x, size.y);
}
IEnumerator scaleObject(Vector2 finalSize)
{
float progress = 0.0f;
Vector2 position = new Vector2 (animationGUI.pixelInset.x, animationGUI.pixelInset.y);
Vector2 size = new Vector2 (animationGUI.pixelInset.width, animationGUI.pixelInset.height);
while (progress < 1.0f)
{
progress += Time.deltaTime/10;
animationGUI.pixelInset = new Rect(position.x, position.y, size.x, size.y);
yield return new WaitForEndOfFrame();
}
}
Thanks in advance.
Answer by paulaceccon · Jan 25, 2013 at 07:29 PM
Well, I could do this with this script:
public delegate Vector2 PositionFunc(Vector2 position, Vector2 sizeIncrement, int step);
public Vector2 scaleBottomRight(Vector2 position, Vector2 sizeIncrement, int step)
{
return new Vector2(position.x-(sizeIncrement.x * (step + 1)), 0f);
}
public Vector2 scaleLeftCenter(Vector2 position, Vector2 sizeIncrement, int step)
{
return new Vector2(0, position.y - (sizeIncrement.y * (step + 1))/2);
}
public Vector2 scaleMiddleCenter(Vector2 position, Vector2 sizeIncrement, int step)
{
return new Vector2(position.x - (sizeIncrement.x * (step + 1))/2, position.y - (sizeIncrement.y * (step + 1))/2);
}
public IEnumerator animateObject(float scale, float timeout, Vector2 position, PositionFunc positionFunc)
{
GameObject animationGO = GameObject.Find("Animation");
if (animationGO)
{
GUITexture animationGUI = animationGO.GetComponent<GUITexture>();
if (animationGUI)
{
float wait = 0.1f;
int steps = (int) ((timeout-1f)/wait);
Vector2 oldPosition = new Vector2 (animationGUI.pixelInset.x, animationGUI.pixelInset.y);
Vector2 oldSize = new Vector2 (animationGUI.pixelInset.width, animationGUI.pixelInset.height);
Vector2 finalSize = oldSize * scale;
Vector2 sizeIncrement = (finalSize-oldSize) / steps;
Vector2 currentSize = oldSize;
Vector2 currentPosition = oldPosition;
for (int i = 0; i < steps; i++)
{
currentSize += sizeIncrement;
currentPosition = positionFunc(position, sizeIncrement, i);
animationGUI.pixelInset = new Rect(currentPosition.x, currentPosition.y, currentSize.x, currentSize.y);
yield return new WaitForSeconds(wait);
}
animationGUI.enabled = false;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613091321im_/https://answers.unity.com/themes/thub/images/avi.jpg)