I'm working on a movement script and it seems there is a big problem with equality/approximation functions. Unity never detects the point I'm looking for unless I involve an own approximation value which is about 0.1 (far too unprecise) and not even that is always detected.
So, as I said I'm having this movement script for a machine. Rightnow, all it shall do is move to coordinates, where it shall go along the x axis first, if it has reached the x destination value go along the z axis and so on. My first idea and attempt was to use the MoveTowards function. That didn't work and I didn't get the point about that problem. But since Google wasn't helpful with that and I ran out of ideas, I was going for Transform.Translate. The translation works fine with it but the machine didn't stop when it should. So I was trying a few things(I will show in the code section) and came up with a solution, in that I compare the current coordinates with these of the destination and check, if they are less than a tolerance value variable I created. The lowest value that would work then was 0.031 but now I'm using 0.1 because the first value only worked sometimes and also 0.1 isn't a sure thing. However, overall that works(most of the time) but it is far too unprecise according to my needs. So I was wondering, where the heck is the problem?
Technical Background and other tries: I'm working with Unity 2019.2.4f1. The models I'm using are probably many years old and first, they were in 3ds. I was also wondering if maybe it's the filetype's problem so I got myself an available stl file, loaded it into Blender and exported it to fbx. I even created a fresh new project to test that but still it's causing the same problem. I was also wondering if that might be a scale kind of problem but scaling the whole machine up and down didn't change anything.
My first attempt with MoveTowards looked something like this. I'm wondering if I should hang on to this because the behaviour was strange. I printed the objects position and the position changed as expected, but only in the debug log, I didn't see the machine moving(also scaled everything up to see if the difference was only too small for my senses)
objY.position = Vector3.MoveTowards(objY.position, destination, speedY * Time.deltaTime);
Then I tried this and I think the problem here was that it was moving but across the destination and further
while(objY.position == destination)
objY.Translate(0, dir * Time.deltaTime, 0);
break;
This is the code I'm using rightnow which kind of works. "unityDistanceTolerance" rightnow is 0.1. This is called when isRunning true is (as you might expect) and it becomes true, when I press Return
private IEnumerator MovementRoutine()
{
if (destination == new Vector3(0, 0, 0))
yield return null;
else
{
while (Mathf.Abs((destination - objY.position).x) > unityDistanceTolerance)
{
MoveX();
yield break;
}
if (Mathf.Abs((destination - objY.position).x) <= unityDistanceTolerance)
{
while (Mathf.Abs((destination - objY.position).z) > unityDistanceTolerance)
{
MoveZ();
yield break;
}
if (Mathf.Abs((destination - objY.position).z) <= unityDistanceTolerance)
{
while (Mathf.Abs((destination - objY.position).y) > unityDistanceTolerance)
{
MoveY();
yield break;
}
}
}
}
isRunning = false;
}
And this is, what I actually want to use, but with this the machine just skips the point and never stops (just like the simple equality comparison)
private IEnumerator MovementRoutine()
{
if (destination == new Vector3(0, 0, 0))
yield return null;
else
{
while (!Mathf.Approximately(destination.x, objY.position.x))
{
MoveX();
yield break;
}
if (Mathf.Approximately(destination.x, objY.position.x))
{
while (!Mathf.Approximately(destination.z, objY.position.z))
{
MoveZ();
yield break;
}
if (Mathf.Approximately(destination.z, objY.position.z))
{
while (Mathf.Approximately(destination.y, objY.position.y))
{
MoveY();
yield break;
}
}
}
}
isRunning = false;
}
I've also tried replacing the Approximately function with this, but same result
while (Mathf.Abs((destination - objY.position).x) > Mathf.Epsilon)
This should be everything I've tried. I'm really wondering why this happens and even more how I can fix that. I hope this is enough input and somebody can help me.