Why do you have to assign what to clamp inside the clamp statement?

for example, MouseX = Mathf.Clamp (MouseX, -90, 90);

whats the point? doesn’t it just clamp itself anyway? why would it ever be different?

how do the two places MouseX appear act different?

You have to assign the clamped value to the original variable because Matf.Clamp takes in “value types”. Value types are “duplicated” when you pass them as parameters into functions so the original value cannot be changed in a normal function like this;

The value of a “value type” variable cannot be changed by passing it to a function*
Please read up on “Value types” and “reference types” :slight_smile: It’ll save you from headaches in the future.

You can try this yourself:

void Start()
{
    int a = 10;
    changeValue(a);
    Debug.Log(a); // still prints out 10 
}

void changeValue(int x)
{
    // the "x" here is actually a completely new variable. "a" doesn't change
    x = x + 100;
}

*You could do this by using “out” or “ref” keywords but I guess it would be unnecessarily complicating the simple task and also this ins not possible in all programming languages so i guess that’s why they chose the simplest/standard way to do it in C# too

void Start()
{
	int a = 10;
	changeValue(ref a);
	Debug.Log(a); // should be 110
}

void changeValue(ref int x)
{
	x = x + 100;
}

Imagine you have a car in your game. You have your foot on the accelerator, so you want the speed to be increasing.

Maybe you’ll do something like:
Speed = Speed + Acceleration;

A (unrealistic) way to to handle giving this car a maximum speed would be to clamp the value.
Speed = Mathf.Clamp(Speed, 0, MaxSpeed);

What this does, is take the speed of your car, and make sure it’s within the min (0) and max (MaxSpeed) speeds that you specify. If the speed was higher than max, it will be set to max. Likewise, if the speed was lower than min, it will be set to min.

With your example, whether or not your Clamp would do anything depends on whether or not your MouseX value will ever go below -90, or above 90. If it does not, then there is no point in clamping. If however, MouseX could have gone beyond your specified range, then your clamp will bring it back within your range.

Clamping meaning Crop the number based on specific range.

for example if MouseX set to 91, then it will returning on the maximum range (it’s meaning 90).

another example if MouseX set to -95, then it will returning on the minimum range (it’s meaning -90).