- Home /
How would you find out if a rotation is between two values?
{
public bool slowMo;
void Start()
{
slowMo = false;
}
void Update()
{
if (transform.rotation.z <= 40 && transform.rotation.z >= -40)
{
slowMo = false;
}
else
{
slowMo = true;
}
if (slowMo == true)
{
Time.timeScale = .3f;
}
else
{
Time.timeScale = 1;
}
}
}
With this code I am meaning to slow down time when the rotation of the block, which this script is attached to, rotates past 40 degrees in either direction, though when I run the game it sets slowMo to true even when the block is sitting vertical. Any help would be greatly appreciated.
Answer by EZhurst · Mar 16, 2015 at 12:46 AM
alright, this seems to work
{
public bool slowMo;
void Update()
{
Debug.Log (transform.eulerAngles.z);
Debug.Log (slowMo);
if (transform.eulerAngles.z >= 210 || transform.eulerAngles.z <= 150)
{
slowMo = true;
}
if (slowMo == true)
{
Time.timeScale = .3f;
}
}
}
thank you raulrsd for your help
Answer by raulrsd · Mar 15, 2015 at 11:33 PM
Yo have a problem with the if statement. No number could be at the same time greater than 40 and less than -40. I think that what you wanted to write was
if (transform.rotation.z >= 40 || transform.rotation.z <= -40)
{
slowMo = false;
}
else
{
slowMo = true;
}
that was a typo, i meant for it to be less than 40 greater than -40, but this doesn't work for me either
I have tried both && and || with the flipped signs and it still doesn't work
I guess that in editor you see that the z rotation of your object is within the desired values, don't you? Also, you could add an f at the end of the 40 and -40 to indicate the compiler that they're a float, like transform.rotation.z (just to be sure).
Additionally, could you write a Debug.Log in the update method to show what is the value of the rotation.z?
I had tried putting an f after the values already, that didn't change anything, and when I watched the inspector, i noticed that the box would go from 0 to 359 degrees because it wraps around so i flipped the box 180 and use >= 150 && <= 210 but got the same result.
And what is the rotation.z value that shows the Debug.Log?