0

I want to get the exact point 0 of the distance between two points without tolerance using Unity.

Update as well as FixedUpdate skip that point unless I'm implementing a tolerance value. I want to avoid that and really get the exact point. Underneath is the code im using right now.

        while (Mathf.Abs((destination - objY.position).x) >= unityDistanceTolerance) 
        {
            DoStuff();
            break;
        }

I want to replace this ">=unityDistanceTolerance" with "==0". Rightnow when I'm doing this, the loop never stops, the distance is never 0. Is this wish even possible? And if so, how?

  • Your wish is not possible for as long as computers use any *ary number system. On binary systems the float, decimal and double data types express numbers with a limited number of bits and so can never make the infinitely small comparisons that would allow a tolerance of 0 to work as expected. The tolerance value accounts for rounding errors created by the mathematical operations on numbers stored in a limited number of bits. – AlwaysLearning Sep 11 '19 at 09:19
  • So, in this case, there's still the question about a more precise way than Update or FixedUpdate. Any suggestions? – Fledermauserl Sep 11 '19 at 09:31
  • Possible duplicate of [Compare floats in Unity](https://stackoverflow.com/questions/39898301/compare-floats-in-unity) – derHugo Sep 11 '19 at 12:03

2 Answers2

1

This is not related to Update, FixedUpdate or in fact to unity 3D at all. Your problem is one of finite precision, both of calculations in terms of physics, but also, on a deeper level, inaccuracies of the number representation. An example stolen from another question on stack

    float f1 = 0.09f*100f;
    float f2 = 0.09f*99.999999f;
    Debug.Log(f1>f2); // returns false

Do not count on the value to be precise

zambari
  • 4,797
  • 1
  • 12
  • 22
1

You might want to try Mathf.Approximately() for comparing two floating point values. As mentioned in the comments, this complication is not related to Update(), FixedUpdate(), Unity3D or even C# at all. It also occurs in other programming languages and there are always workarounds (like Mathf.Approximately or your implementation) rather than perfect precision solutions.

Taken from docs.unity3d

Floating point imprecision makes comparing floats using the equals operator inaccurate. For example, (1.0 == 10.0 / 10.0) might not return true every time. Approximately() compares two floats and returns true if they are within a small value (Epsilon) of each other.

  • Thank you, I tried it out. Looks a little like the machine giggles :D I think I will keep my solution then. – Fledermauserl Sep 11 '19 at 10:48
  • You're welcome @Fledermauserl. Before you move on with your solution I should add that **Mathf.Epsion** is equal to **1.401298E-45**, or at least it *is* when you Debug.Log(Mathf.Epsilon) on my computer. Hope this can be a helpful viewpoint for you to compare Mathf.Approximately() and your method precision-wise. Cheers. – madbuggerswall Sep 11 '19 at 11:06
  • Oh wow, I didn't know that. I'll try to work with it. Thank you! – Fledermauserl Sep 11 '19 at 11:36
  • `Mathf.Approximately(x,y)` basically equals `Mathf.Abs(x-y) <= Mathf.Epsilon` ;) Still ... for freely moving objects in 3D space there still might never be a frame for which this is really `true` depending on the velocities and on your `DoStuff();` ... – derHugo Sep 11 '19 at 11:58
  • Unfortunately "Mathf.Abs((destination - objY.position).x) > Mathf.Epsilon" will not be registered... – Fledermauserl Sep 11 '19 at 11:59
  • kind of frustrating – Fledermauserl Sep 11 '19 at 11:59
  • and that is the point I meant. It feels like Update skips that point, but I'm not sure if thats really the point. – Fledermauserl Sep 11 '19 at 12:01
  • That would be too much since theres a bunch of methods that belong to it but what I can say is, when I used my own Variable (I found out by trying out, the smallest I got was 0.031) it worked (but also not at every start of the project) but for my taste it was too unprecise. But when I try to use Mathf.Epsilon the point is not detected. Basically its about a mesh that shall simply move along the X Axis, and at that point I'm talking about, it shall move along the Z Axis.Now it just moves across – Fledermauserl Sep 11 '19 at 12:07
  • and to get to Mathf.Approximately again. it is the same problem – Fledermauserl Sep 11 '19 at 12:11
  • Well as I said .. it is very unlikely that your moving object has "exactly" the same position in a certain frame ... What if in the one frame it is still e.g. 0.1 units before your destination and in the next frame it is already behind it? As you don't show your `Update` code we can't see how exactly you are moving your object but you should use `MoveTowards` instead for avoiding overshooting... – derHugo Sep 11 '19 at 13:24
  • I'm hoping that I can get it down to the basics tomorrow. But where shall I post it? Here in the comments? I would pretty much like to use MoveTowards instead but somehow this doesn't work, too. The thing where the debugger shows its moving but I don't see it in the scene... – Fledermauserl Sep 11 '19 at 14:41