They grow up to be so big

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;
	}
}
And another trickey bit. If you are creating a bunch of objects in a loop, like I create a grid of objects, you can grow them one at a time by yielding to the groObj function, or you can have them all grow in at once simply by calling grow Obj;
example of having objects “grow” into the scene one at a time (prefab is a variable in your script, and you have dragged and dropped a prefab onto that script)

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

  • LinkedIn
  • Delicious
  • Reddit
  • Twitter
  • Slashdot
  • Digg
  • Facebook
  • Blogger Post
  • StumbleUpon
  • Yahoo Bookmarks
  • MySpace
  • Windows Live Favorites
  • Share/Bookmark

2 Responses to “They grow up to be so big”

  1. cinematic says:

    Cool. Nice meeting you at union.
    In audio, growObj could be accompanied by fading up the prefab theme.

  2. Noir says:

    Hey, it was great seeing you as well. thanks for the positive feedback!

Leave a Reply

You must be logged in to post a comment.

Content recommendations from Evri