- Home /
Problem with C# "if" statement?
Hi! I am currently creating a game wherein my character rotates 90/-90 degrees everytime I hit the corresponding button. Here are some parts of the code:
// going towards Z-axis
if(lookVector.z == 1 || lookVector.z == -1)
{
planePosition.x = mainCamera.transform.position.x;
planePosition.z += planeObject.renderer.bounds.size.z * lookVector.z;
print ("Z: " + planePosition.z);
}
// going towards X-axis
else if(lookVector.x == 1.0 || lookVector.x == -1.0)
{
planePosition.z = mainCamera.transform.position.z;
planePosition.x += planeObject.renderer.bounds.size.z * lookVector.x;
print ("x: " + planePosition.x);
}
else
{
Debug.Log ("hey " + lookVector);
}
My character's "transform.forward" starts out as (0,0,1.0) and since I rotate him 90 degrees everytime, then either the X or Z is bound to be 1 or -1, if I am not mistaken?
However here's the problem, I try to rotate my character thrice: left right then left again, and the output is as follows:
x: 475.9813
Z: 563.7064
hey(-1.0,0.0,0.0)
Unity printed the "hey" part, which was not supposed to happen since my X coordinate was -1. The same happens when I rotate him in a R-L-R sequence. However there are also times when it does work normally(when I rotate him randomly). Can someone please tell my why?
Answer by Coderdood · Jun 16, 2013 at 05:51 PM
I suspect that the reason is because you are trying to compare floats using == . The reason this doesn't work involves dull, technical details about what floating point numbers actually are and if your interested you can read more on Wikipedia.
The use of the equality test (if (x==y) ...) requires care when dealing with floating point numbers. Even simple expressions like 0.6/0.2-3==0 will, on most computers, fail to be true[39] (in IEEE 754 double precision, for example, 0.6/0.2-3 is approximately equal to -4.44089209850063e-16). Consequently, such tests are sometimes replaced with "fuzzy" comparisons (if (abs(x-y) < epsilon) ..., where epsilon is sufficiently small and tailored to the application, such as 1.0E−13). The wisdom of doing this varies greatly, and can require numerical analysis to bound epsilon.
In order to fix it try this:
//if(lookVector.z == 1 || lookVector.z == -1)
//Replace with
if ((Mathf.Approximately(lookVector.z, 1.0)) || (Mathf.Approximately(lookVector.z, -1.0)))
//else if(lookVector.x == 1.0 || lookVector.x == -1.0)
//Replace with
else if((Mathf.Approximately(lookVector.x, 1.0)) ||(Mathf.Approximately(lookVector.x,-1.0)))
Note some other users have advised that Mathf.Approximately might not be good enough. See this answer and this answer for more details.
Ok that fixed it for now :) I'll try to use some other more reliable methods than $$anonymous$$athf.Approximately next time, but thanks a lot Coderdood!
Answer by Eric5h5 · Jun 16, 2013 at 05:39 PM
If it gets to the "else" condition, that means none of the previous conditions were true. What is "lookVector"? Comparing floats to exact values is problematic. Maybe you want to round the number off before comparing.