How do you make an object larger as it moves?

This is for my snowball game again. As the snowball is rolling down the hill it is meant to get bigger. I want its size to increase only if the up arrow is pushed and its overall size is still less than 100. I've been using a while loop but it's not working. Any suggestions?

1 Answer

1

Try this:

var snowball : Transform;        // link your snowball prefab here
var growthRate : float = 10.0;   // rate at which the snowball grows
private var growthAmount : float = 0.0;  // growth amount calculated every frame

function Update ()
{
   if ( snowball )   // if the snowball exists
   {
      if (Input.GetKey ("up")        // if the "up" key is held
       && snowball.localScale.x <= 100) // and the snowball scale is <= 100
      {
         growthAmount = growthRate * Time.deltaTime;   // calculate growthAmount
         snowball.localScale += new Vector3(growthAmount,growthAmount,growthAmount); // apply the growthAmount to the localScale
      }
   }
}

This might not fit perfectly with what you have, but as you didn't post any code, I started from scratch! I've used Time.deltaTime in the calculation to make it time-independent, and you can tune the growthRate to your needs. Hope this helps - any questions, please comment!

I tried using and returned this error: Assets/Scripts/Growth.js(10,31): BCE0051: Operator '<=' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'int'. Help? :S

thankyou so much, works perfectly

Marowi, can I ask what you mean when you said you were comparing the localScale vector in an int?

Oh alright! That makes sense. Thank you for clarifying.