- Home /
How To Check If X Rotation Is Between Two Numbers??
I'm making a game where an animation would only play if the player's x rotation is between 2 numbers. Something like this(JavaScript Please):
if (Player x rotation is between 0 and 180)
{
if (Input.GetKeyDown(KeyCode.Space))
{
animation.playe("Vault");
}
}
I know this isn't how the x rotation would be, but that's the kind of thing im looking on how to do. Does anyone know how to do this? (In JavaScript).
This can be a hard problem to solve. Is the 'x' axis the only axis that the object is rotated on?
I meant to say the y axis. The player only rotates on the y axis, except for when playing the animation, it then rotates on the x or z axis.
Answer by robertbu · Jan 06, 2014 at 08:53 PM
If the object is only rotating on the 'y' axis, you can use eulerAngles. Assuming the script is on the player, the code would look like:
if (transform.eulerAngles.y >= 0 && transform.eulerAngles <= 180) {
// Do stuff
}
But this is not a general solution. It will fail to work in many situations outside the specific situation you outline here (only 'y' rotation between 0 and 180). A slightly better solution (that also can have issues) might be:
if (Vector3.Angle(transorm.forward, Vector3.forward) <= 90.0) {
// Do stuff
}