Vector2D/3D: Need more than one decimal place in debugger

Only having one decimal place displayed for the components of Vector3D and Vector2D in the debugger (e.g., locals) can be a pain, depending on that scale you’re working at. An option for specifying the number of places would be useful. (And, yes, I realize there are “manual” ways of printing out the full value).

I feel your pain. This is slightly “manual,” but you only have to do it once: add an extension method to the Vector3 struct and use that to format your string:

public static class Vector3Ext
{
    public static string Log(this Vector3 v)
    {
        return v.x + " " + v.y + " " + v.z;
    }
}

Call Debug.Log(myVector.Log()) instead of Debug.Log(myVector), and you get more decimal places.

1 Like

Thank you for the response. That looks helpful but what I’m really after (perhaps I wasn’t clear) is not having to call anything at all. I’d simply like to hover over a Vector3 variable in the source or in the “locals” list while debugging and see a reasonable number of decimal places.

Yes, that is a different issue. Are you debugging in Visual Studio? If you float over the Vector3 and then float over the triangle that opens the drop-down, you will see the x, y, and z members with precision, not rounded to one place.

Here’s a cap:

4292797--384739--Debug.png

Thinking about the fact that Vector3s are rounded to one decimal when you float over them in the debugger got me wondering about their ToString method. Looking at the reference source, it’s just what you would expect from what you see in the debugger:

        public override string ToString()
        {
            return UnityString.Format("({0:F1}, {1:F1}, {2:F1})", x, y, z);
        }

Alas, there appears to be nothing one can do about that, and it would also appear that this is what the debugger shows you (though you can still drill down, as I mentioned).

Interestingly, this additional override is also supplied:

        public string ToString(string format)
        {
            return UnityString.Format("({0}, {1}, {2})", x.ToString(format), y.ToString(format), z.ToString(format));
        }

This means you don’t need to use anything as exotic as an extension method when you call Debug.Log. Just call Debug.Log(myVector.ToString("R")), and you’ll get the full precision.

Now, one further discovery is worth mentioning in the context of a discussion about Vector3 and decimal places: Unity has apparently decided that two vectors are identical if, in actual fact, they are almost identical. This interesting bit of code appears near the top of the class definition:

        // *Undocumented*
        public const float kEpsilon = 0.00001F;
        // *Undocumented*
        public const float kEpsilonNormalSqrt = 1e-15F;

That first constant shows up in the comparison operator override, causing it to return true when two vectors are below the threshold set by kEpsilon (the actual test is a little subtler than just looking at the vector components individually, but it amounts to the same thing: two vectors can be arithmetically different, with Unity reporting they are equivalent).

I had not known this before, so I’m glad you asked your question. If you are dealing with issues that have you concerned with the values of Vector3 components at the level of several decimal places, it might be knowledge you can use as well.

(In fairness to Unity, their API documentation on the == operator does document this behavior.)

Thanks again for the response.
Re: the implementation of the ToString() method, it seems like they’d just need to pull the format specifiers from a Setting/Preferences property rather than being hardcoded to “F1” and all would be well.

The basic issue is that I’m working down at the meter and sub-meter level, stepping frame by frame and observing a particular Vector2 or Vector3 variable. 10cm resolution doesn’t cut it.

Do you have to be at such a small scale? Unless physics is involved, Unity is dimensionless. Could you just scale up and avoid the issue that way? (I’m guessing Unity isn’t making it easy to operate at a small scale because they want to discourage us from doing so. When physics is involved, scale matters quite a bit.) Anyway, if you are debugging in Visual Studio, you can see the full precision. You just have to float your mouse in the right place.