# Oscillate an object's scale

I am able to scale an object using a scaling function I wrote. Internally it builds matrices that are multiplied by each of the vertices to transform them. The parameters I give the scaling function tell it how much to scale the object by as a factor of its current scale.

I want my object's scale to smoothly oscillate between two values, so I have used Sin waves to build my scaling factor, which is:

1 + amplitude * (Mathf.Sin(2 * Mathf.PI * frequency* Time.time) - Mathf.Sin(2 * Mathf.PI * frequency * (Time.time - Time.deltaTime)))


The result looks very close to what I want, but with each oscillation, the object gets slightly smaller than before.

The problem is that my function is written so that it scales the vertices as a factor of its 'current' scale, which is always changing.

• is it not possible to add a parameter to your functions to get its original scale also? and use that in the method where needed (or make global variables that hold references to the original scale values – Big T Larrity Aug 15 '17 at 12:32
• Certainly, but I'm interested in solving this problem out of interest. – Ashley Aug 15 '17 at 12:36
• ah sure! well, my brain can only think that the solution is to hold a reference to the original scale and use that instead of the its current scale – Big T Larrity Aug 15 '17 at 12:41
• Id just set it back to its original scale upon every completion of the cycle i think . but im certainly no mathematician. good luck im sure you'll find the answer in the end :) – Big T Larrity Aug 15 '17 at 12:46

Generally, you always apply transformations to the original unscaled/untransformed object.

Theoretically, in your case, since you are simply calculating a scaling factor, and under the assumption that it's never zero, you can always invert the scale transform by using the inverse of the old scaling factor before applying the new one.

Something like this (pseudocode):

if (/* sOld isn't set */)
{
sOld = 1;
}

sOldInverse = 1/sOld;     // assuming sOld is never 0
s = (result of your function);

s = sOldInverse * s;  // the new scalling factor
sOld = s; // store somewhere until the next update


However, in practice you might run into issues with numerical precision; since you don't have the original scale, tiny errors may accumulate over time, leading to wrong results.