Truncate a 2D texture

Hello everyone.

I simply would like to know if there is a way to truncate a 2D texture (in the GUI), in order to create a drawn health gauge that cannot be repeated. I tried many methods, but they all scale the texture, and don’t truncate it.
I searched for a long time but couldn’t find this basic function.

I would be really grateful if someone knows a solution.

This can be done with GUI.BeginGroup() method.

Thank you, I managed to truncate by moving the texture on the X axis inside a Group. Unfortunatly, there is a new problem with that…

I use this formula to calculate the x axis.

divisionVie = (life / lifeMax * 334);
GUI.Label (Rect(-334 + Mathf.Round(divisionVie), 0, 334, 84), textureVieBarre);

I have to use the Round function to have the closest integer. But it seems that this function only works on numbers, and not on variables. On variables, it always changes the float to… a 0.
I don’t know why. I think I’ll have a headache -_-’

Make sure your variable actually has the value you think it does. Sounds like its <1.

If the multiplication is done before the division, yes, that could have been an explanation. But I tried to change it, and it’s not the problem.

This code returns 25.5 :

But this one…

It returns 0!
He can’t even do a simple division. I really don’t understand…

I managed to do what I wanted by multiplying the first member of the division (so it still rounds the result, but don’t returns 0 anymore).
But I still don’t understand why Unity rounds a division in a float.

If anyone knows why, I’m interested.

Remember 27/40 is less than 1. That means as an int it can get truncated to 0.

27 and 40 are both ints. Therefore 27(int) / 40(int) is 0(int).
try dividing 27.0 with 40.0.
I don’t use Javascript often, I prefer C#, but I suggest that you explicitly state your types. Otherwise you will end up with Javascript choosing something you don’t want.

The other option is to mutliply both values by 1000 before dividing and hten removing hte 1000:

((27 * 1000) / (40 * 1000)) / 1000

It’s clunky but it is another way of getting higher accuracy if you need ints, and may lose precision partway through the calculation.