# How to Scale an object up and down over time?

Hi,

I have this code that should scale an object up and down over a series of time.

My problem is however that it reaches its max scale and does not set the boolean to switch the statement to scale down.

``````if (transform.localScale.x < mMinScale.x && !mScaleUp) //
{
transform.localScale = Vector3.Lerp(transform.localScale, -mMinScale, 2 * Time.deltaTime);
print("HELLO");
}
if (transform.localScale.x < mMaxScale.x && mScaleUp)
{

transform.localScale = Vector3.Lerp(transform.localScale, mMaxScale, 2 * Time.deltaTime);
print("GOODBYE");
}
if (transform.localScale.x == mMaxScale.x)
{
mScaleUp = false;
print(mScaleUp);
}
``````

Any ideas?

Kind Regards

New code is

``````	if (transform.localScale.x > mMinScale.x && !mScaleUp)
{
transform.localScale = Vector3.Lerp(transform.localScale, -mMinScale, 2 * Time.deltaTime);
print("HELLO");
}
if (transform.localScale.x < mMaxScale.x && mScaleUp)
{
// transform.localScale = //Time.deltaTime
transform.localScale = Vector3.Lerp(transform.localScale, mMaxScale, 2 * Time.deltaTime);
print("GOODBYE");
}
if (transform.localScale.x >= mMaxScale.x)
{
mScaleUp = false;
print(mScaleUp);
}

else if (transform.localScale.x <= mMinScale.x)
{
mScaleUp = true;
print(mScaleUp);
}
``````

Hey!

Why not try using the Sin function to create a repetitive motion such as a pulsating object? I feel this is a perfect opportunity to use one!

In this example script we will have your transform object to have its size move from 1 to -1:

``````using UnityEngine;

public class Scaling : MonoBehaviour {

void Update ()
{
Vector3 vec = new Vector3(Mathf.Sin(Time.time), Mathf.Sin(Time.time), Mathf.Sin(Time.time));

transform.localScale = vec;
}
}
``````

If you do not want your object to go from -1 or 1 then you can add numbers to your sin function like so:

``````using UnityEngine;

public class Scaling : MonoBehaviour {

void Update ()
{
Vector3 vec = new Vector3(Mathf.Sin(Time.time) + 1, Mathf.Sin(Time.time) + 1, Mathf.Sin(Time.time) + 1);

transform.localScale = vec;
}
}
``````

That script will make your object grow from 0 to 2 and then back again.

You can change the speed at which it pulsates by multiplying your time variable by some number, for example:

``````using UnityEngine;

public class Scaling : MonoBehaviour {

void Update ()
{
Vector3 vec = new Vector3(Mathf.Sin(Time.time * 2), Mathf.Sin(Time.time * 2), Mathf.Sin(Time.time * 2));

transform.localScale = vec;
}
}
``````

That script would make it pulsate at twice the speed! Aren’t Sin functions great? You could also make it 0.5 or a smaller fraction to make it pulsate slower.

You can change the amplitude of your cube by multiplying the entire function by some number, for example:

``````using UnityEngine;

public class Scaling : MonoBehaviour {

void Update ()
{
Vector3 vec = new Vector3(2 * Mathf.Sin(Time.time), 2 * Mathf.Sin(Time.time), 2 * Mathf.Sin(Time.time));

transform.localScale = vec;
}
}
``````

That script would make your transform pulsate from -2 to 2. If you were to multiply it by 0.5 or a smaller fraction, it would pulsate at a smaller pace.

Thanks, I hope you learned something and good luck with your programming endeavours!

This seems to work perfectly. If you dont want it to scale on a certain axis, simply set the scaling speed on that axis to 0.

``````[Header("Max scale")]
public Vector3 mMinScale;
public Vector3 mMaxScale;
public Vector3 mScaleSpeed;

private Vector3 currentScaleSpeed;

void Start()
{
currentScaleSpeed = mScaleSpeed;
}

void Update()
{
if (transform.localScale.x <= mMinScale.x || transform.localScale.x >= mMaxScale.x)
{
currentScaleSpeed.x *= -1; //invert it
}

if (transform.localScale.y <= mMinScale.y || transform.localScale.y >= mMaxScale.y)
{
currentScaleSpeed.y *= -1; //invert it
}

if (transform.localScale.z <= mMinScale.z || transform.localScale.z >= mMaxScale.z)
{
currentScaleSpeed.z *= -1; //invert it
}

transform.localScale = transform.localScale + currentScaleSpeed * Time.deltaTime;
}
``````

Change “==” to “>=” on line 12.

On line 1, change “if (transform.localScale.x < mMinScale.x && !mScaleUp)” to “if (transform.localScale.x > mMinScale.x && !mScaleUp)”…
Rest looks fine, it should work… @littlebigprogramming

I made a tutorial for this on my YouTube channel - YouTube it only requires a couple lines of code