我试图比较C#中的两个值,但是结果却很奇怪。
//Getting the pre-defined rotation value - 45 , 0 or 315 degrees as a float
float rot = GetComponent<Transform>().rotation.eulerAngles.z;
Debug.Log(rot.ToString());
if (rot == 45)
{
Debug.Log("IN 45");
GetComponent<Rigidbody2D>().velocity = new Vector3(2f, -bulletSpeed, 0);
}
else if (rot == 315)
{
Debug.Log("IN 315");
GetComponent<Rigidbody2D>().velocity = new Vector3(-2f, -bulletSpeed, 0);
}
else
{
Debug.Log("IN 0");
GetComponent<Rigidbody2D>().velocity = new Vector3(0, -bulletSpeed, 0);
}
当rot等于315时,结果将进入第二个if语句,但是当rot等于45时(由Unity Console选项卡显示),结果将进入第3个if语句,而不是第1个
现在,如果我将第一条语句从与整数“ 45”相比的浮点数“ rot”转换为与字符串“ 45”相比的字符串“ Rot的值”,则该函数将按预期工作。
对此有任何见识。