- Home /
iTween Scale up then down again
Heyho,
im currently trying to implement a scaler for buttons, on MouseEnter it should scale up and on MouseExit it should return to the original value.
onMouseEnter: iTween.ScaleBy(gameObject, iTween.Hash("x", 1.05, "z", 1.05, "default", .1));
onMouseExit: iTween.ScaleBy(gameObject, iTween.Hash("x", 0.95, "z", 0.95, "default", .1));
The problem with my current code is, that it will become smaller and smaller over lots of OnMouseEnter / OnMouseExit's.
Example: Stating value: 1200 after scaling it up: 1260 after scaling down again: 1197
Got any ideas? Regards
Answer by Paulo-Henrique025 · Aug 14, 2012 at 12:16 PM
I think your solution is to use iTween.ScaleTo(). When you ScaleBy() the expected behavior is exactly what you are describing, the button will scale up +5 OnMouseEnter and them -5 OnMouseExit, if there is a transformation occurring during a new mouse event the ScaleBy will Add ou Subtract the value from the current scale which is in middle of a transformation.
When you use ScaleTo no matter the current scale it will scale to the desired size.
Answer by Lex · May 30, 2016 at 01:44 AM
I know it's a veeeeeery old question but I couldn't resist to comment on this.
The problem here is simple math, just follow me:
If you scale 100 up 5% you'll get this: 100 * 1.05 = 105
If you scale the result down 5% you'll get this: 105 * 0.95 = 99.75
Can you see? When you're working with percentages you can't just add and subtract the same % and expect it to come back to where it was.
So what you actually need is to scale down so it goes back to the original value. What you'll need is the following:
onMouseEnter: iTween.ScaleBy(gameObject, iTween.Hash("x", 1.05, "z", 1.05, "default", .1));
onMouseExit: iTween.ScaleBy(gameObject, iTween.Hash("x", 95.2380f, "z", 95.2380f, "default", .1));
And you might still get accumulative error, but I guess this time it won't be visible.
Answer by carloalbores123 · Mar 10, 2019 at 04:56 AM
I'm sorry to revive this thread but for the sake of others who also encounter this (like me) up to this date, the probably easiest method is as @Paulo-Henrique025 answered. Just use iTween.ScaleTo instead of ScaleBy. This solved mine as it scales up / down nearly the desired Vector 3 values.
Make sure to have it like this:
Vector3 desiredSize = new Vector3 (desired_x,desired_y,desired_z);
float t = 1f;
iTween.ScaleTo(gameobject, desiredSize, t);
just replace the values you needed..