- Home /
Vector3.Normalize not working for particular input?
Hi,
Did I break unity?
Here is the code:
Vector3 ax2 = new Vector3(0.3f, 1.0f, 0.0f);
ax2.Normalize();
Debug.Log(ax2);
Here is the log output: (0.3, 1.0, 0.0) //Not normalized!
And for this code:
Vector3 ax2 = new Vector3(2.0f, 1.0f, 1.0f);
ax2.Normalize();
Debug.Log(ax2);
The output is: (0.8, 0.4, 0.4) // Normalized!
Same behaviour with ax2.normalized;
It drives me crazy. What am I missing?
I'm running: 2020.3.22f1 release
Answer by Bunny83 · Nov 29, 2021 at 05:43 PM
Try this instead:
Vector3 ax2 = new Vector3(0.3f, 1.0f, 0.0f);
Debug.Log("original length: " + ax2.magnitude);
Debug.Log(".ToString: " + ax2);
Debug.Log(".ToString(\"F1\"): " + ax2.ToString("F1"));
Debug.Log(".ToString(\"F6\"): " + ax2.ToString("F6"));
ax2.Normalize();
Debug.Log("normalized length: " + ax2.magnitude);
Debug.Log(".ToString: " + ax2);
Debug.Log(".ToString(\"F1\"): " + ax2.ToString("F1"));
Debug.Log(".ToString(\"F6\"): " + ax2.ToString("F6"));
You will get is this::
original length: 1.044031
.ToString: (0.3, 1.0, 0.0)
.ToString("F1"): (0.3, 1.0, 0.0)
.ToString("F6"): (0.300000, 1.000000, 0.000000)
normalized length: 1
.ToString: (0.3, 1.0, 0.0)
.ToString("F1"): (0.3, 1.0, 0.0)
.ToString("F6"): (0.287348, 0.957826, 0.000000)
Please have a close look at the results and make sure you interpret them correctly ^^
Answer by Ivan_Voloshin · Nov 30, 2021 at 02:41 AM
Thanks! It was a string formating problem! The vector was normalized indeed. But due to only 1 decimal place in ToString() I couldn't see that. Interestingly is that if in debugger you highlight mouse cursor above the Vector3 object it will also show the result of ToString. Being a C++ programmer I'm not used to this and fell into the trap. Lesson learned.