Why does this truncated Vector 2 have 2 decimal places

No description
41 Replies
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
No description
Memw
Memw6mo ago
you mean in the debug log?
Memw
Memw6mo ago
that's most likely why it does even tho it's truncated, they remain floats, and the ToString() method formats them with the F2 format option, and by default I think Debug.Log transforms everything using the ToString() method before printing it, which is what Console.WriteLine in normal C# does, and what makes most sense
TimberStalker
TimberStalker6mo ago
You can use Vector2Int if you want it to have integer parts I think it fits your use case more
Memw
Memw6mo ago
Yeah, if you don't need floating point arithmetic use Vector2Int
TimberStalker
TimberStalker6mo ago
Also, Math is the default c# math class that works on doubles. Mathf is provided by unity and works on floats. Since floats are smaller it runs faster, so i recommend you switch to using Mathf instead.
Memw
Memw6mo ago
it doesn't really do anything else, it doesn't run faster, it's just a wrapper so you don't need to be casting values every time
TimberStalker
TimberStalker6mo ago
Really?
Memw
Memw6mo ago
basically, it internally uses the C# Math API
TimberStalker
TimberStalker6mo ago
ok then
Memw
Memw6mo ago
GitHub
UnityCsReference/Runtime/Export/Math/Mathf.cs at master · Unity-Tec...
Unity C# reference source code. Contribute to Unity-Technologies/UnityCsReference development by creating an account on GitHub.
Memw
Memw6mo ago
Here is the source if you want to check that out well, it contains some more methods, but for the methods that are common between these 2 APIs, it uses the C# Math API I love the fact that they are making pull requests to a reference repository, and they make a lot of work for it to never be checked out 😹
TimberStalker
TimberStalker6mo ago
News to me. I thought it was different, especially since Microsoft added their own MathF class,
Memw
Memw6mo ago
it's different
No description
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
I have just now realized that the reson I couldnt find truncate under the MathF class is because I was using the popup option Mathf
Memw
Memw6mo ago
well, basically it's what's missing from the Math api that The unity MathF has but the C# Math doesn't
TimberStalker
TimberStalker6mo ago
Oh ya, Mathf and MathF are different classes real fun
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
just incase c# was too straightforward
TimberStalker
TimberStalker6mo ago
Remember, if you can do something, you must be able to do it in 5 almost indistinguishable ways
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
I have changed them out to ints, but I get the same problem
TimberStalker
TimberStalker6mo ago
you have to change the base type to Vector2Int
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
did that are 2 and 2.00 interchangeable?
Memw
Memw6mo ago
the 2.00 is just string formatting the internal value is simply 00000010
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
ah ok
TimberStalker
TimberStalker6mo ago
should be. If you want to log it differently you can do $"({vector2int.x}, {vector2int.y})" this will avoid tostring on the vector and call tostring on the ints directly
TimberStalker
TimberStalker6mo ago
@41phaNum3r1ca1 Actually another thing. Truncate will cause a bug if x or y is negative. You should be using Floor instead Truncate(-1.3) = -1 Floor(-1.3) = -2
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
the former is what I want 👍
Memw
Memw6mo ago
Isn't truncating values just casting them to ints?
TimberStalker
TimberStalker6mo ago
Are you sure? With truncate the the tiles left and right(and up or down) of the axes will be given the sime tile position
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
wouldnt floor have the same problem?
TimberStalker
TimberStalker6mo ago
no
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
if -1.3 floored is 2, it will be in the same position as 2
TimberStalker
TimberStalker6mo ago
that is correct let me draw a diagram itl help understand
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
ok
TimberStalker
TimberStalker6mo ago
No description
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
ah, I was confused because you had made a typo I understand what you mean now
TimberStalker
TimberStalker6mo ago
oh i missed the negatives thats my fault sorry
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
I probably will not change it, as the way I have it creating will always be in one quadrant. Regardless, thank you for your help
TimberStalker
TimberStalker6mo ago
If its all positive then you should be fine
Want results from more Discord servers?
Add your server