4

I am working on a game that rotates an object on the z axis. I need to limit the total rotation to 80 degrees. I tried the following code, but it doesn't work. minAngle = -40.0f and maxAngle = 40.0f

Vector3 pos = transform.position;
pos.z = Mathf.Clamp(pos.z, minAngle, maxAngle);
transform.position = pos;
Imapler
  • 1,401
  • 12
  • 26
jadkins4
  • 903
  • 7
  • 18
  • 34

2 Answers2

9

The code you posted clamps the z position. What you want is to use transform.rotation

void ClampRotation(float minAngle, float maxAngle, float clampAroundAngle = 0)
{
    //clampAroundAngle is the angle you want the clamp to originate from
    //For example a value of 90, with a min=-45 and max=45, will let the angle go 45 degrees away from 90

    //Adjust to make 0 be right side up
    clampAroundAngle += 180;

    //Get the angle of the z axis and rotate it up side down
    float z = transform.rotation.eulerAngles.z - clampAroundAngle;

    z = WrapAngle(z);

    //Move range to [-180, 180]
    z -= 180;

    //Clamp to desired range
    z = Mathf.Clamp(z, minAngle, maxAngle);

    //Move range back to [0, 360]
    z += 180;

    //Set the angle back to the transform and rotate it back to right side up
    transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, z + clampAroundAngle);
}

//Make sure angle is within 0,360 range
float WrapAngle(float angle)
{
    //If its negative rotate until its positive
    while (angle < 0)
        angle += 360;

    //If its to positive rotate until within range
    return Mathf.Repeat(angle, 360);
}
Imapler
  • 1,401
  • 12
  • 26
  • Thanks for this method. It pretty much solved my problem! The only thing was if I allowed rotation past -50 or 50, the rotation would freeze and not respond to mouse movement. I made sure I kept these limits, and it works great! I appreciate your help. – jadkins4 Sep 18 '14 at 20:04
  • GENIUS! There are so many answers around the web to this question but none of the solutions work except this one. BRILLIANT! – Ralph B Sep 24 '14 at 21:00
1

Here's a static version of the nice solution by Imapler that, instead of changing the angle itself, it returns the campled angle, so it can be used with any axis.

public static float ClampAngle(
    float currentValue,
    float minAngle,
    float maxAngle,
    float clampAroundAngle = 0
) {
    return Mathf.Clamp(
        WrapAngle(currentValue - (clampAroundAngle + 180)) - 180,
        minAngle,
        maxAngle
    ) + 360 + clampAroundAngle;
}

public static float WrapAngle(float angle)
{
    while (angle < 0) {
        angle += 360;
    }
    return Mathf.Repeat(angle, 360);
}

Or if you don't expect to use the WrapAngle method, here's an all-in-one version:

public static float ClampAngle(
    float currentValue,
    float minAngle,
    float maxAngle,
    float clampAroundAngle = 0
) {
    float angle = currentValue - (clampAroundAngle + 180);

    while (angle < 0) {
        angle += 360;
    }

    angle = Mathf.Repeat(angle, 360);

    return Mathf.Clamp(
        angle - 180,
        minAngle,
        maxAngle
    ) + 360 + clampAroundAngle;
}

So now you can do:

transform.localEulerAngles.x = YourMathf.ClampAngle(
    transform.localEulerAngles.x,
    minX,
    maxX
);
Parziphal
  • 6,222
  • 4
  • 34
  • 36