- Home /
 
Increase/Decrease Object Scale
Hello,
I want to be able to smoothly increase/decrease the scale.y of my object.
Basically, I have a variable that will randomly range between 1 and 10 every second, and I want this variable to effect the scale.y of a certain object.
In other words, if var random = 5, then scale.y will also =5, if the var random changes to, say 8, then scale.y will smoothly/gradually increase to 8 too.
How will this be achieved? Lerp?
Here is where I'm at:
 function Start(){
     
     while(true){
         oldScale = sizeY;
         sizeY = Random.Range(1, 11);
         newScale = sizeY;
 
         yield WaitForSeconds(0.2);
     }
 }
 
 private var speed : float = 1;
 
 var oldScale : int;
 var newScale : int;
 
 var oldScaleVec : Vector3;
 var newScaleVec : Vector3;
 
 function Update (){
 
     oldScaleVec = Vector3(1, oldScale, 1);
     newScaleVec = Vector3(1, newScale, 1);
 
      cube.transform.localScale = Vector3.Lerp(oldScaleVec, newScaleVec, Time.deltaTime / 2);
 
 }
 
               This kind of works, although it's not animating it - more like jumping straight to the new given value.
have you tried to do it with the Lerp function and give it the randomly choosen variable each second ?
Answer by fafase · Apr 15, 2013 at 11:06 AM
 float newScale;
 float ratio;
 
 void Start(){
     ratio = Time.deltaTime;
     InvokeRepeating("ChangeScale",1f,1f);
 }
 
 void Update(){
     float newVal = Mathf.Lerp(transform.localScale.y,newScale,ratio);
     transform.localScale = new Vector3(transform.localScale.x,newVal,transform.localScale.z);
 }
 void ChangeScale(){
     newScale = Random.Range(0f,10f);
 }
 
              Hi there, I've tried converting this into JavaScript, and I get an error: UnityEngine.$$anonymous$$athf.Lerp(float, float, float) is not compatible with the argument list (UnityEngine.Vector3, float, float) ?
Thank you! Works fine. FYI: within your Update function, you need to replace the end y with a z.
Your answer
 
             Follow this Question
Related Questions
Why won't my object grow? 1 Answer
Still confused on lerps... 1 Answer
Make gameobject a child without affecting scale? 1 Answer
Dynamic text on object 1 Answer
Huge objects dissapear 1 Answer