I’m just getting my feet wet again with Unity and C#. I’ve run into a problem I hope I can get some help with. I have a list of custom objects called “LineSegments”. The LineSegments are each a couple of Vector2’s. I’m trying to find the first match in my list. It works sometimes but not all the time. I can’t find any reason for this. I’m thinking I’ve written it incorrectly. Could someone tell me if this lamda statement looks correct?
Replacing with a foreach was a good idea. It still doesn’t work. So something in my data is screwy. Thanks for the help, I should have thought of that. :S
== between two Vectors will only be true if the difference (distance) between them is less than 1e-5 (floating point imprecision), maybe it’s floating point fluctuation?
Is actually comparing two vectors directly, not checking for its hash value?
Indeed, do to floating imprecision, I typically subtract two vectors and get squared magnitude, then compare against tolerance, for example 0.0001f.
== compare against Distance and true if the difference is less than 0.00001f
VectorX.Equals goes for the exact match
VectorX.Distance(VectorX a, VectorX b) == (a-b).magnitude
So you can use simply the VectorX.Distance() method if you’re going for the 0.0001f precision, if you’re okay with the 0.00001f, then use == operator.
Personally I would be avoiding using distance (magnitude), if not need to use it.
It uses square root, which is “complex” math.
There is another magnitude parameter, which is not squared.
Or simply sum of raised to power 2 elements.
Either way, with custom comparison, you can set resolution as you like, if you need it.