I'm trying to compare two values in C#, however I'm getting a weird result.
Here it goes:
//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);
}
Now the weird behavior is as follows:
When rot is equal to 315, the result will go in the 2nd if statement, however when rot is equal to 45 (which is shown by the Unity Console tab), the result will enter the 3rd if statement instead of the 1st.
Now if i convert the first statement from a float "rot" compared with int "45" to string "Value of Rot" compared with string "45", the function works as intended.
Any insights on this.