I suppose that title is enough but in case if it’s not:
I want to make something like: If ship’s rotation is larger than -20 AND smaller than 20, then… blah, blah… How do i make this numerical range work better and make it cleaner?
Here’s what i have done by now: (it works but not as i want it to)
//Return Z-Axis to default position
if (transform.rotation.z > 180 && transform.rotation.z < 210 && !horRotation){
transform.Rotate (Vector3.forward * 1.5 * -1);
}
if (transform.rotation.z < 180 && transform.rotation.z > 150 && !horRotation){
transform.Rotate (Vector3.forward * 1.5);
}
}
transform.rotation is a Quaternion - its x, y and z components aren’t the familiar angles you see in the Inspector field Rotation (it’s a very often mistake - which I made too in the past). The property that appears in Rotation is transform.localEulerAngles. You could place transform.localEulerAngles.z in a temporary variable (easy writing and is more efficient) and compare it to the limits:
//Return Z-Axis to default position
var zAngle = transform.localEulerAngles.z;
if (zAngle > 180 && zAngle < 210 && !horRotation){
transform.Rotate (Vector3.forward * 1.5 * -1);
}
if (zAngle < 180 && zAngle > 150 && !horRotation){
transform.Rotate (Vector3.forward * 1.5);
}
But it seems to bring the rotation back to 180 degrees if it’s between 150 and 210. Is it what you really want? If you want to return to 0 degrees, you can do this:
//Return Z-Axis to default position
var zAngle = transform.localEulerAngles.z;
if (zAngle > 180) zAngle -= 360; // transform 181..360 to -179..0
if (zAngle < 30 && !horRotation){
transform.Rotate (Vector3.forward * 1.5 * -1);
}
if (zAngle > -30 && !horRotation){
transform.Rotate (Vector3.forward * 1.5);
}