- Home /
Using lerp to scale
Hi, I'm want to use lerp for scaling an object. Intially it's scale is (0,0,0) and I'm changing it to desired scale by lerp.
gameObject.transform.localscale = Vector3.Lerp(gameObject.transform.localscale, scaling, 0.2);
Currently its only giving me 0.2 of scaling.I want it to change over time by 0.2 and make it to 'scaling' finally(Like in Mathf.Lerp). Not sure how to do this. Any ideas?
Answer by Tarlius · May 30, 2013 at 03:57 AM
Read the documentation. You need to vary the t. Fixing it at 0.2 will always give you a vector 20% from the first to the second parameters. Depending on the desired effect, you will want something like:
public void DoScale(Vector3 start, Vector3 end, float totalTime) {
StartCoroutine(CR_DoScale(start, end, totalTime));
}
IEnumerator CR_DoScale(Vector3 start, Vector3 end, float totalTime) {
float t = 0;
do {
gameObject.transform.localscale = Vector3.Lerp(start, end, t / totalTime);
yield return null;
t += Time.deltaTime;
} while (t < totalTime)
gameObject.transform.localscale = end;
yield break;
}
From your question it sounds like you want it to change 0.2 per second, which means you'll want a total time of 5.