I have a script which triggers a movement when a rotation is completed. The script casts the Euler angle of a transform to an int
. If it is true
, the condition is executed. For some weird reason, the code in the if
condition does not execute, even if the condition is true
. I have confirmed the condition is true
through both my compiler and the debug log.
Here is my code:
if(rotationInitiate)
{
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles,
new Vector3(0, 0, angleOfRotation), Time.deltaTime * speedOfRotation);
Debug.Log(transform.eulerAngles.z);
if((int)transform.eulerAngles.z == 180)
{
Debug.Log("Hurray we have successfully executed the if statement");
// More rotation stuff
}
}
As you can see by my log, the angle returns an exact value of 180, but does not enter the if
statement to perform the second log output:
Why might my if
statement not be executing?
Mathf.Abs(transform.eulerAngles.z -180.f) < 1.0f
? Also make sure the debug output is something unique so that it is not mistaken for some other, forgotten somewhere else, debug output. – wondra Jan 29 '17 at 11:20angleOfRotation
is 180 and thespeedOfRotation
is 10. – Ikun Jan 29 '17 at 11:40Update
method must only be beig called approx. 10 or less times per second, as that is the only situation where you could pass a value of 1 or greater into theSlerp()
method. In turn, this is the only way you could immediately reach the rotation of 180, unless you already were at the rotation of 180. – Gnemlock Jan 29 '17 at 12:38