Why use movementFactor?

Hello!

Math isn’t my strongest subject, so apologies if this sounds like a silly question but, why do we declare the variable: [Range(0,1)] [SerializeField] float movementFactor; at the start of our code, and then in Update() move the platform by setting movementFactor to:

        movementFactor = rawSineWave / 2f + 0.5f;
        Vector3 offset = movementFactor * movementVector;
        transform.position = startingPos + offset;

Would it be incorrect to remove movementVector altogether, and calculate the offset by doing:

        Vector3 offset = rawSineWave * movementVector;
        transform.position = startingPos + offset;

I know that by doing this, when I set movementVector, in code or in the Inspector, to something like
[SerializeField] Vector3 movementVector = new Vector3(0f, 10f, 0f);
that my platform will now move 10f in both directions, as opposed to 5f in each, but if anything this makes more sense to me.

Thanks in advance,
Sincerely,
Not mathematically inclined

Hi,

Admittedly, this topic is a bit tricky. It took me some time to wrap my mind around Ben’s solution, so don’t worry if you didn’t grasp everything at once either. :slight_smile:

I recently wrote a rather lengthy explanation of the sine wave and the idea behind Ben’s solution in this thread.

The y-values the raw sine wave returns range from -1 to 1. We want them to range from 0 to 1. With rawSineWave / 2f + 0.5f, we modify the wave/function to get the desired result. By y-values, I mean the values on the vertical axis. See the linked thread.


If you find this too confusing, maybe it helps to ignore the sine stuff for a moment and try to understand what the goal is. The goal is to interpolate between two coordinates, A and B.

// o is our object, in the calculation the variables stand for coordinates
// o = position of of, etc.

Ao-----------------------------------B      // o = A + 0 * offset = A
A-----------------o------------------B      // o = A + 0.5 * offset
A-----------------------------------oB      // o = A + 1 * offset = B

You probably recognised the pattern. o = A + x * offset where o is the target position of object o, A our starting position, and x the variable ranging from 0 to 1.

With the sine wave, we calculate the value for x. Alternatively, we could have used a linear function or something else.

Ben wrote it in a slightly different way and named the offset movementVector. I prefer offset times the parameter x (or t). The linear interpolation is a very important function in programming.

Did this clear it up for you?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms