- Home /
Smooth Scaling of an Object
Hi, I'm trying to smooth scale my object by using zoom;
So everytime I scroll up / zoom in it will become bigger and vice versa.
For now I'm using this code
for(GameObject child in listDraw)
{
float onChangeZoom1 = currentZoom - originZoom
if (currentZoom != originZoom)
{
child.transform.localScale = child.transform.localScale *
Mathf.Pow(2, onChangeZoom1);
originZoom = currentZoom;
}
}
I tried using the Lerp or MoveTowards function, but it never did scale the object.
if(currentZoom > originZoom)
child.transform.localScale = Vector3.Lerp(child.transform.localScale,
child.transform.localScale * 2, Time.deltaTime * .02f);
else
child.transform.localScale = Vector3.Lerp(child.transform.localScale,
child.transform.localScale / 2, Time.deltaTime * .02f);
Any way to solve this?
Answer by Captain_Pineapple · 4 days ago
Are you sure that this is actually not doing anything? Did you really check the numerical values of the scale if they changed?
Because from your code (second version) this should work. However the change each frame would be really really small. So from a visual perspective probably nothing changed. You current code (still the second version) would only increase by 2% over 1 second IF you were constantly scrolling for the complete second. So the thing you should check first is to remove the factor 0.02f
from your Lerp statements.
Ah got it, lemme try it. I thought that the third statement was for the speed and not the resizing itself, and wanted to put .02f so that the speed will be slow. Didn't think it'll affect the resizing itself.
Yes it is for speed only. But Lerp works like this:
newVal = Lerp(A, B, step)
where newVal will then be
newVal = A + (B-A)*step;
So when you run 60 frames per second and your object has an original size of 1 then after one frame you'd have a size of around 1.0032 as your step would be (1/60)*0.02
. You would not see this difference.
So my assumption is that your speed is only way too slow.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Scale (Transform.localscale) a gameobject based on ARFaceAnchor anchorData 0 Answers
Issue lerping localscale 1 Answer
Zoom / enlarge gameobject ? 2 Answers
Unity streching sprite gameobject to fit two positions. 1 Answer