Smooth Scaling

I’m trying to make a script that will let me adjust the dimensions of a box and have them smoothly transform from the old dimensions to the new dimensions. This is what I’ve come up with, but it doesn’t do anything at all… not even generate errors.

var width = 2.5;
var height= 1;
var depth = 3;

private var oldsize = Vector3(width, height, depth);

function FixedUpdate() {
	var usersize = Vector3(width, height, depth);
	
	if (transform.localScale != oldsize) {
		transform.localScale = Vector3.Slerp(oldsize, usersize, Time.deltaTime * 2);
		oldsize = usersize;
	}
}

Any ideas on what’s wrong or how to do this better?

Try this:

var newScale : Vector3 = Vector3 (1, 1, 5);
var speed : float = 2.0;

function Update ()
{
     transform.localScale = Vector3.Lerp (transform.localScale, newScale, speed * Time.deltaTime);
}
5 Likes

Wow… that worked excellently, thank you. It even used less code… :smile:

1 Like

Hi Daniel,
If I want to make smooth scrolling of texture in screen, which coding part should I change?
Im able to make it move, but it stopped after my finger relief. I want to make it continue scroll few second after my finger relief.

if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
scrollPosition.x += touch.deltaPosition.x;
lastSwipe = SwipeDirection.None;
lastSwipeTime = 0;
couldBeSwipe = true;
startPos = touch.position;
startTime = Time.time ;
break;

case TouchPhase.Moved:
//if (Mathf.Abs(touch.position.x - startPos.x) > comfortZone)
//{

if (Mathf.Abs(touch.position.x - startPos.x) > comfortZone)
{
scrollPosition.x += touch.deltaPosition.x * Time.deltaTime;
couldBeSwipe = true;
rigidbody.AddForce(scrollPosition * speed * Time.deltaTime);

}

break;

case TouchPhase.Ended:
if (couldBeSwipe)
{
float swipeTime = Time.time - startTime;
float swipeDist = (new Vector3(0, touch.position.x, 0) - new Vector3(0, startPos.x, 0)).magnitude;

if ((swipeTime < maxSwipeTime) (swipeDist > minSwipeDist))
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0)
{
lastSwipe = SwipeDirection.Right;
PlayerPrefs.SetInt(“swiping”,1);
scrollPosition.x += touch.deltaPosition.x * Time.time;
}
else if (swipeValue < 0)
{
lastSwipe = SwipeDirection.Left;
PlayerPrefs.SetInt(“swiping”,2);
scrollPosition.x += touch.deltaPosition.x * Time.time;
}
lastSwipeTime += Time.deltaTime * 5;
}
}
break;
}

Vector3 scale =new  Vector3(0.025f,0.025f,0.025f);

if (_Bttn [i].GetComponent<RectTransform> ().localScale.x <= 1.5f) {

                    _Bttn [i].GetComponent<RectTransform> ().localScale += Vector3.Lerp (scale, _Bttn [i].GetComponent<RectTransform> ().localScale, Time.deltaTime * 0.5f);


                }

It must be work more smooth Scale