- Home /
C# Code working on PC won't work on Mac's
Hey guys, I got a strange problem with my code.
I tried to move an object and after it reached its destination, I want to move it into a defined position (in my example, -100 for all 3 coordinates).
It works fine on the PC, but won't work correct on Apple Products (Mac, iPad etc...).
My Code looks like
...
while( MyObject.transform.position != targetDestination){
t += Time.deltaTime / duration;
MyObject.transform.position = Vector3.Lerp(MyObject.transform.position, targetDestination, t);
yield return 0; // suspend execution till next frame
}
print ("after the while");
MyObject.transform.position = new Vector3(-100f, -100f, -100f);
yield return new WaitForSeconds(0.2f);
Like I said, on PCs, this works excellent, on Macs, it won't. On Apple products it seems to end after the while{} since the "after the while" won't appear on the consol. This lines are executed in a coroutine.
Does anybody know why whats the problem here?
Answer by Xtro · Aug 07, 2013 at 03:11 PM
OMG :) You can't test if the position equals to destination. It never will be the same value. In 3D world, we are dealing with float numbers and they are not real mathematical floats.
Destination check is more complex that just checking against the equality of the coordinates. Even if it works on PC, it's based on luck and you should never trust it.
Easiest way to make a destination check is the distance measurement. Get the distance between the positions. If it's smaller than a predefined distance limit, you can say it reached the destination.
don't forget that distance measurement will fail if the object moves too fast. Because it can skip the detection area in one game update loop.
Thanks dude, that was excactly the solution. But on the other hand, I think its kinda strange that it worked every time on the PC and never on the $$anonymous$$ac, since it aparently allways rounded wrong o_O'
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Ios Tap Function 1 Answer
moving to iOS- XML files 2 Answers
could players to play music from itunes in my app? 0 Answers