Hi
I’m facing issue with eulerAngles of my object. What i need to do is to make a max rotation angel that can’t pass it. one using time till reach 0 value. other have max angel.
public float z_max;
// Update is called once per frame
void Update () {
z_max = gameObject.transform.eulerAngles.z;
if (gameObject.transform.eulerAngles.z >= 30.0f) {
gameObject.transform.eulerAngles.z = 30f; // max angel is 30
}
if (gameObject.transform.eulerAngles.z <= -30.0f) {
gameObject.transform.eulerAngles.z = Time.deltaTime; // count till 0 ?
}
}
Clamping via eulerAngles isn’t effective due to the fact euler angles don’t work like that.
350 degrees is a valid angle that might come out, but that’s technically -10 degrees as well, so you might end up setting your angle to 30 when it’s -10
if there is rotation around x or y, z is wayyyy not accurate to your clamping. For example if you rotate to 90 around the x-axis, you’ve locked the y and z gimbals, and any rotation around y is the same as any negative rotation around z. This implication means that if there is any rotation around the x, your z might return a less than accurate value relative to your desired clamping.
If you ONLY plan to rotate around 1 axes, then point 2 won’t matter. And what you can do is normalize your z angle before modifying it. Basically pass your z value into this wrap function:
public static float Wrap(float value, float max, float min)
{
max -= min;
if (max == 0)
return min;
return value - max * (float)Math.Floor((value - min) / max);
}