Should I use Clamp without the xClampedPos variable?

Hi, just wondering, does this code work as well as the one shown in the tutorial?
Should I use the “in between” float for Clamp? Clamping the original float seems to be working as well or is this not a good practice?

//my code
float newXPos = transform.localPosition.x + xOffset;
newXPos = Mathf.Clamp(newXPos, -xRange, xRange);

Rather than like in the video

//from tutorial
float rawXPos = transform.localPosition.x + xOffset;
float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

Hi Dinate,

Welcome to our community! :slight_smile:

In many cases, there are multiple ways to make something work in Unity, and Rick cannot show all of them. If your code works, it’s a solution by definition. I think Rick felt that the code was more readable if he used a new variable and named it clampedXPos. That name is indeed self-explanatory but newXPos is also fine within the context. Given your solution works, it’s a matter of personal preference.


See also:

Thank you for your support.

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

Privacy & Terms