Moving a platform back and forth.

I am trying to make a moving platform for my game that moves in one direction and after a certain distance reverses the direction (repeating). While this code would normally be pretty easy I wanted it so that its speed was relative to the way a spring operates.

I tried to use a combination of transform.rotate and transform.translate because transform.rotate goes from
-pi to pi, then pi to -pi, and repeats which could give the desired transform.translate effect if i converted that to speed.

Look into the Vector3.SmoothDamp method:

Like a spring?

I know that the easing algorithm ‘elastic’ looks a bit like a spring:

The algorithm for which is:

        #region Elastic Ease
        public static float ElasticEaseIn(float t, float b, float c, float d)
        {
            return ElasticEaseInFull(t, b, c, d, 0, 0);
        }

        public static float ElasticEaseOut(float t, float b, float c, float d)
        {
            return ElasticEaseOutFull(t, b, c, d, 0, 0);
        }

        public static float ElasticEaseInOut(float t, float b, float c, float d)
        {
            return ElasticEaseInOutFull(t, b, c, d, 0, 0);
        }

        public static float ElasticEaseInFull(float t, float b, float c, float d, float a, float p)
        {
            float s;
            if (t == 0) return b; if ((t /= d) == 1) return b + c;
            if (p != 0) p = d * 0.3f;
            if (a != 0 || a < Math.Abs(c)) { a = c; s = p / 4; }
            else s = p / _2PI * (float)Math.Asin(c / a);
            return -(a * (float)Math.Pow(2, 10 * (t -= 1)) * (float)Math.Sin((t * d - s) * _2PI / p)) + b;
        }
        public static float ElasticEaseOutFull(float t, float b, float c, float d, float a = 0, float p = 0)
        {
            float s;
            if (t == 0) return b;
            if ((t /= d) == 1) return b + c;
            if (p != 0) p = d * 0.3f;
            if (a != 0 || a < Math.Abs(c)) { a = c; s = p / 4; }
            else s = p / _2PI * (float)Math.Asin(c / a);
            return (a * (float)Math.Pow(2, -10 * t) * (float)Math.Sin((t * d - s) * _2PI / p) + c + b);
        }
        public static float ElasticEaseInOutFull(float t, float b, float c, float d, float a = 0, float p = 0)
        {
            float s;
            if (t == 0) return b; if ((t /= d / 2) == 2) return b + c;
            if (p != 0) p = d * (0.3f * 1.5f);
            if (a != 0 || a < Math.Abs(c)) { a = c; s = p / 4; }
            else s = p / _2PI * (float)Math.Asin(c / a);
            if (t < 1) return -.5f * (a * (float)Math.Pow(2, 10 * (t -= 1)) * (float)Math.Sin((t * d - s) * _2PI / p)) + b;
            return a * (float)Math.Pow(2, -10 * (t -= 1)) * (float)Math.Sin((t * d - s) * _2PI / p) * .5f + c + b;
        }
        #endregion

Which you can find here with several of the other algorithms (as well as my tween engine):

Ease methods are called as a function of time and length, where the 4 parameters are:

currentTime (t) - the currentTime into the ease we are, each from tally up the change in time and pass it in here

initialValue (b) - the starting point, say you wanted to move from 5 to 15, 5 is the start value.

totalChange (c) - the amount of change that should occur from initialValue, so from 5 to 15, 10 is the totalChange

duration (d) - the total amount of time that the ease takes place, when currentTime = duration it should return initialValue + totalChange.