Why does this truncated Vector 2 have 2 decimal places

No description
41 Replies
41phaNum3r1ca1
41phaNum3r1ca1OP7mo ago
No description
Memw
Memw7mo ago
you mean in the debug log?
Memw
Memw7mo 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
TimberStalker7mo ago
You can use Vector2Int if you want it to have integer parts I think it fits your use case more
Memw
Memw7mo ago
Yeah, if you don't need floating point arithmetic use Vector2Int
TimberStalker
TimberStalker7mo 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
Memw7mo 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
TimberStalker7mo ago
Really?
Memw
Memw7mo ago
basically, it internally uses the C# Math API
TimberStalker
TimberStalker7mo ago
ok then
Memw
Memw7mo 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
Memw7mo 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
TimberStalker7mo ago
News to me. I thought it was different, especially since Microsoft added their own MathF class,
Memw
Memw7mo ago
it's different
No description
41phaNum3r1ca1
41phaNum3r1ca1OP7mo 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
Memw7mo ago
well, basically it's what's missing from the Math api that The unity MathF has but the C# Math doesn't
TimberStalker
TimberStalker7mo ago
Oh ya, Mathf and MathF are different classes real fun
41phaNum3r1ca1
41phaNum3r1ca1OP7mo ago
just incase c# was too straightforward
TimberStalker
TimberStalker7mo ago
Remember, if you can do something, you must be able to do it in 5 almost indistinguishable ways
41phaNum3r1ca1
41phaNum3r1ca1OP7mo ago
I have changed them out to ints, but I get the same problem
TimberStalker
TimberStalker7mo ago
you have to change the base type to Vector2Int
41phaNum3r1ca1
41phaNum3r1ca1OP7mo ago
did that are 2 and 2.00 interchangeable?
Memw
Memw7mo ago
the 2.00 is just string formatting the internal value is simply 00000010
41phaNum3r1ca1
41phaNum3r1ca1OP7mo ago
ah ok
TimberStalker
TimberStalker7mo 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
TimberStalker7mo 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
41phaNum3r1ca1OP7mo ago
the former is what I want 👍
Memw
Memw7mo ago
Isn't truncating values just casting them to ints?
TimberStalker
TimberStalker7mo 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
41phaNum3r1ca1OP7mo ago
wouldnt floor have the same problem?
TimberStalker
TimberStalker7mo ago
no
41phaNum3r1ca1
41phaNum3r1ca1OP7mo ago
if -1.3 floored is 2, it will be in the same position as 2
TimberStalker
TimberStalker7mo ago
that is correct let me draw a diagram itl help understand
41phaNum3r1ca1
41phaNum3r1ca1OP7mo ago
ok
TimberStalker
TimberStalker7mo ago
No description
41phaNum3r1ca1
41phaNum3r1ca1OP7mo ago
ah, I was confused because you had made a typo I understand what you mean now
TimberStalker
TimberStalker7mo ago
oh i missed the negatives thats my fault sorry
41phaNum3r1ca1
41phaNum3r1ca1OP7mo 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
TimberStalker7mo ago
If its all positive then you should be fine
Want results from more Discord servers?
Add your server