Having objects just “appear” may be cool on stage, but in a game, it’s kinda lame. I needed my game objects to “grow” into place so they would catch the players eye.
How to do that in Unity3d? well the unity 3d web forums gave me a hint.
Bottom line is to use the Coroutine function yeild, and the Lerp function off of Vector3D.
what I do is instantiate an object from a prefab, then call a function called growObj.
Here is my GrowObj function (NOTE: I leave off the type of the first parameter so it could be called with any object that contains a .transform member)
/* groObj function, grow an object fom nothing to some destination size over a period of time
go is an object to grow
toScale is the destination scale preferably the object's original local scale
*/
function growObj( go, toScale: Vector3)
{
var i = 0.0;
var rate = 1.0 / GrowTime;
var StartScale = Vector3.zero;
while ( i <= 1.0)
{
i += Time.deltaTime * rate;
go.transform.localScale = Vector3.Lerp(StartScale, toScale, i);
yield;
}
}
Objects grow one at a time
for (var z = zStart ; z < zEnd; z++ ){
var pos = Vector3 (0, 0, z) * 4;
var clone;
clone = Instantiate(prefab, pos, Quaternion.identity);
var DestScale : Vector3 = clone.transform.localScale; // capture the original size of the object
yield growObj(clone,DestScale);
}
all objects appear and grow together
for (var z = zStart ; z < zEnd; z++ ){
var pos = Vector3 (0, 0, z) * 4;
var clone;
clone = Instantiate(prefab, pos, Quaternion.identity);
var DestScale : Vector3 = clone.transform.localScale; // capture the original size of the object
growObj(clone,DestScale);
}
Yup the only difference in the two loops is the yield statement in front of growObj.
That’s all for now. I hope to make a “bubble” grow function some time, where the object will overshoot destination scale once, then undershoot the destination scale once
Cool. Nice meeting you at union.
In audio, growObj could be accompanied by fading up the prefab theme.
Hey, it was great seeing you as well. thanks for the positive feedback!