Question on mathf.clamp

Does anyone know if I can use mathf.clamp only on x or y coordinates? I checked the internet and I don’t see any examples. All of them use both x and y.

Hi,

Mathf.Clamp works for integers and floats. If x and y are of type int or float, you can use the method to clamp their respective value.

ok but Nina, my question goes deeper. I found a youtube video and managed to make use of it. I limited the movement of my object so it can’t go out of the screen. This was the objective of the video. So now I’m asking if there’s such a thing as using mathf.clamp for x ONLY. Or Y ONLY. If there is can someone help by showing an actual example? Is there one on youtube? Or has someone asked this before?

I don’t know if anybody has ever asked this before. Generally, programming is rather thinking on a general/abstract level. Just because WE used Mathf.Clamp for variables named x and y, doesn’t mean that you cannot use it for anything else. Just because WE develop a specific game with Unity doesn’t mean that you can use Unity for this specific game only.

Imagine I gave you a fork and you saw a video where somebody ate spaghetti with it, does that mean you can use the fork ONLY for eating spaghetti? Or could you eat other food, too? The fork is nothing but a tool. You could even use it for gardening.

It’s a tool just like Mathf.Clamp and everything else.

You can use it for what you can use it. And if you don’t know if you can use it, test it. You are limited to what you can do, not to what somebody told you what you could do.

int blah = 999;
int abc = Mathf.Clamp(blah, 0, 99);

I didn’t use x and y anywhere in this valid example, did I?

You can find more examples in the API.

Did this clear it up for you, @anon37455979?


See also:

Well, I don’t see any examples for x or y only so I’ll consider that x and y are both required. If you see any simple examples of x or y only, let me know.

Here is an example with x and y only.

int x = 1;
int y = 2;

x = Mathf.Clamp(x, y);
y = Mathf.Clamp(x, y);

ok thanks. If you see examples using x without y or y without x, let me know.

Here is an example without x and y:

And here is another one:

int a = 1;
int b = 2;

a = Mathf.Clamp(a, b);
b = Mathf.Clamp(a, b);

Nina,
You misunderstood me yet again. I want x only or Y only because i want to limit the objects to move either up and down OR left and right. My purpose is to get some tutorial to write my own app. Understand? I can’t find this anywhere in youtube or other websites. So your example - if you can think of one, should have x only without y OR y only without x. I found examples with x and y but not x only or y only. I hope this is clear.

Julian,

If you feel I misunderstood you, please be a bit more patient with me and explain again what you meant.

In the Block Breaker and Laser Defender project, we restricted the player’s movement with Mathf.Clamp.

Vector3 pos = transform.position;
pos.x = Mathf.Clamp(pox.x, 1f, 10f);
pox.y = Mathf.Clamp(pos.y, 1f, 10f);
transform.position = pos;

If you don’t want to clamp the x or the y value, remove the respective line.

ok thanks. I will try this out later.

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

Privacy & Terms