Why not use Mathf.Abs() and why put a const in Update()?

In line 26 of the code in the video you’ve elected to use:

movementFactor = (rawSinWave + 1f) / 2f;

Why not use the Mathf.Abs() function to get the absolute value?

movementFactor = Mathf.Abs(rawSinWave);

For those who don’t know, an absolute value is always the positive value of the input for example:
if x = 1, the absolute value will be 1. If x = -1, the absolute value is still 1.

Also, a constant shouldn’t be declared in Update(). Why is it not up with the other variable declarations?

1 Like

Hi @AdamT,

Welcome to our community! :slight_smile:

That’s an interesting question. Let’s take a look at the difference between the two versions. I usually use https://www.desmos.com/ to visualise graphs.

Blue is Rick’s function, red yours:

image

As you can see, the waves are not the same, thus the movement will look different. Test your solution. Maybe you like that movement better. In the end, it’s a matter of personal preference.

Feel free to test the functions yourself on Desmos.

y=\frac{\left(\sin x+1\right)}{2} (Rick’s)
y=\operatorname{abs}\left(\sin\left(x\right)\right) (yours)

image

Maybe this link works.

Did this help?


See also:

5 Likes

Ah! Thank you so much. The graph helped visually see the difference too, much appreciated.

1 Like

I’m glad I could help. :slight_smile:

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

Privacy & Terms