Home > front end >  C# - Multiplying Vector3 by float value returns rounded value
C# - Multiplying Vector3 by float value returns rounded value

Time:08-09

Unity2020 This string:

Debug.Log(new Vector3(1, 0, 0) * 1.25f);

returns (1.3, 0.0, 0.0). Why?

CodePudding user response:

It is just how the ToString override works. You can specify how the numbers are format by using different overrides. For example:

var vector = new Vector3(1, 0, 0) * 1.25f;
Debug.Log(vector.ToString("F3"));

Or individually log out the values:

var formatString = "F3";
Debug.Log($"Vector is X:{vector.X.ToString(formatString)}, Y:{vector.Y.ToString(formatString)}, Z:{vector.Z.ToString(formatString)}");

CodePudding user response:

Because the Vector3 is converted to a string and 1 digit precision is Unity 2020's default.

If you want something else you need to explicitly call Vector3.ToString with a format specifier.

Debug.Log((new Vector3(1,0,0) * 1.25f).ToString("F2"));
  • Related