Mathf.Floor/Editor Common Practices
I'm creating my own level editor and I'm drawing lines to create a grid for a TileMap. I'm having trouble understanding some common practices I've seen in comparison to the way I am currently using Mathf.Floor.
public float height = 32f;
My code:
for (float x = 200; x < 400; x += height)
{
Mathf.Floor(x);
}
//First 3 results:
// 200
// 232
// 264
Other code:
for (float x = 200; x < 400; x += height)
{
Mathf.Floor(x/height) * height;
}
//First 3 results:
// 192
// 224
// 256
I've noticed that either way in solving this issue, the increment is still going to be 32 units between each result. The resulting grid at the intersects will still produce "perfect" 32x32 squares within the grid. To me, it seems that the resulting values are arbitrary and the only factor necessary to have is the proper increment.
Why is it that dividing and multiplying by height is so common as seen in the second method when using Mathf.Floor?
Why you do flooring in first example? All the outputs would be integer already.
These were just values I decided to plug in for the explanation. I could have added any float but chose not to for the sake of easy reading.
$$anonymous$$athf.Floor(x/height) * height;
is a simple way to align the the grid to 0 and force the value to be a multiple of height
0, 32, 64, 96, 128, 160, 192, 224, 256
while $$anonymous$$athf.Floor(x);
does not align the grid to 0
8, 40, 72, 104, 136, 168, 200, 232, 264
and also does not force the value to be a multiple of height
This is exactly the explanation I was looking for. In my examples where I tried to convince myself they were the same thing I would never include 0. I completely left out the idea of alignment to 0. Thanks for the help.
Answer by Bonfire-Boy · Oct 30, 2015 at 04:58 PM
The second version ensures that the number you come up with is an integer multiple of height; specifically, the integer multiple of height that's closest to x and less than x.
I'm not convinced that it is all that common to be honest. It would account for a tiny fraction of the many uses I've put Floor operations to over the years. But here's a use case...
Say you have a number of equally spaced horizontal floors, with dist
being the distance between them.
You could then use Mathf.Floor( yPos/dist ) * dist
to find the height of the floor that's below an arbitrary object (the height of the object being yPos
). The floor, in other words, that the object would hit first it if fell down.
In the scenario you provided, why would one want to use $$anonymous$$athf.Floor if the absolute distance was to be expected? e.g. Something is 10.8ft away you wouldn't say it's only 10ft away.
I'm sorry, I don't understand your question. I'm talking about finding the height of the floor that an object would fall to. So for example (using your numbers) if the object is at 10.8ft and the floors are at heights of 10, 20, 30ft etc, then the formula gives you 10ft.
Your answer
Follow this Question
Related Questions
Assigning multiple assets to multiple variables on inspector 1 Answer
Camera Preview is empty 5 Answers
Assets folder not loading 1 Answer
Unity editor crashing on Intel graphics on Macbook Pro 0 Answers