- Home /
Some syntax nuances I don't really understand
float w = (1f / Screen.width) * Input.mousePosition.x;
Vector3 mousepos = new Vector3((1f / Screen.width) * Input.mousePosition.x, 1f / Screen.height * Input.mousePosition.y);
It's written exactly the same. But float w
is not the same as mousepos.x
Why tho? I thought vector3 is just a bowl of 3 floats. :/
Cheers!
Not really a question so I won't answer, but you're just seeing a rounded version of the Vector3's components. You're right about the definition as it's literally a struct with 3 float fields, so there's no magic going on other than cleanly printing to the console.
Thats true. The internal value is still 0.488... the console just presents a rounded value.
You can print out Vector3s with higher precision using .ToString. Check out https://docs.unity3d.com/ScriptReference/Vector3.ToString.html
Answer by Tripleganger · Feb 26, 2020 at 06:08 PM
Hi @Twinklier !
Vector3 automatically rounds up or down your floats, so you don't have to deal with those tiny decimals. And Vector3 is a struct with 3 floats!
Edit: improvements to the answer suggested by Bunny83.
Well, your answer is a bit misleading. Vector3 is not an array, it's a struct with 3 float values. It also does not always round the values up. It just rounds the values (up or down) depending on the decimal places. Without an explicit format string Unity just uses the number format `"F1"` by default. For more information see C# / .NET number format strings.
Note that Vector3 implements what is called an "indexer" which you might have interpreted as "array". For more information on indexer, see Indexers
Your answer
Follow this Question
Related Questions
C# Question Regarding"new" keyword and Vector3 type 2 Answers
Adding curve to Vector3.MoveTowards 1 Answer
Using collision with Vector3.MoveTowards 3 Answers
Vector3 == comparison efficiency and float precision 5 Answers
compare current Vector3.position with stored initial Vector.3 position in c# 1 Answer