Hey guys,
so that’s my script:
public class Scale : MonoBehaviour {
Vector3 temp;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
temp = transform.localScale;
temp.y += 1f;
temp.x += 1f;
transform.localScale = temp;
}
}
a simple script to scale an object. What I want to add is: after like 0.2 seconds the scaling should stop and go back to its standard size of the cube. How am I gonna implement that?
You should look into Coroutines, and please, use code tags.
Here’s something I made a while ago when I was bored. Just place it on a cube with a collider.
public float LerpTime = 3f;
public Vector3 ScaleTo = new Vector3(3f, 3f, 3f);
private bool _coroutineRunning; //Just to prevent odd behavior if user clicks object during lerptime
public void Update ()
{
if(Input.GetMouseButtonDown(0) && !_coroutineRunning)
{
Debug.Log("Clicked!");
StartCoroutine(scaleCube(LerpTime, ScaleTo));
}
}
private IEnumerator scaleCube(float lerpTime, Vector3 scale)
{
_coroutineRunning = true;
Vector3 currentScale = transform.localScale;
float elapsedTime = 0f;
//Gradually exapnd object
while (elapsedTime < lerpTime)
{
transform.localScale = Vector3.Lerp(currentScale, scale, (elapsedTime / lerpTime));
elapsedTime += Time.deltaTime;
yield return null;
}
elapsedTime = 0f;
//Gradually contract object to original size
while (elapsedTime < lerpTime)
{
transform.localScale = Vector3.Lerp(scale, currentScale, (elapsedTime / lerpTime));
elapsedTime += Time.deltaTime;
yield return null;
}
//Just to eliminate any floating point errors and ensure the object returns to its original size
transform.localScale = currentScale;
_coroutineRunning = false;
}