# How to use a negative value when MathF.Clamping a eulerAngle?

So I’m trying to get my platform to be able to rotate in between the values of -25 and 25 along both the X and Z axis, but whenever the x/z rotation value is <= 0 it resets back to 25.

This is my current code

``````        // Rotate Level Forward
if (Input.GetKey(KeyCode.W))
{
gameObject.transform.Rotate(new Vector3(Time.deltaTime * xRotationSpeed, 0, 0));
}

// Rotate Level Left
if (Input.GetKey(KeyCode.A))
{
gameObject.transform.Rotate(new Vector3(0, 0, Time.deltaTime * zRotationSpeed));
}
// Rotate Level Backwards
if (Input.GetKey(KeyCode.S))
{
gameObject.transform.Rotate(new Vector3(-Time.deltaTime * xRotationSpeed, 0, 0));
}
// Rotate Level Right
if (Input.GetKey(KeyCode.D))
{
gameObject.transform.Rotate(new Vector3(0, 0, -Time.deltaTime * zRotationSpeed));
}

_xClamp = Mathf.Clamp(transform.eulerAngles.x, xRotationCapNeg, xRotationCapPos);
_zClamp = Mathf.Clamp(transform.eulerAngles.z, zRotationCapNeg, zRotationCapPos);

gameObject.transform.eulerAngles = new Vector3(_xClamp, transform.rotation.y, _zClamp);
``````

All within my ProcessInputs() function

Something to consider: Unity uses Quaternions for rotations, not Euler Angles/Degrees.

Those rotations, however, are then translated back into Euler Angles for your benefit, as they’re easier for human comprehension. Likewise, you are generating a rotation, converted from Euler Angles into a Quaternion rotation, which is then converted back for your benefit.

With this in mind, it means that the rotation can be represented in a variety of ways when described in degrees. For example, 350 degrees and -10 degrees would be the same amount of rotation on a given axis, so that’s what would be offered up in the conversion from Quaternion to Euler rotation.

One example of how you can work around this is to instead apply the rotation as an absolute relative to a given baseline (for example, no rotation would be Quaternion.identity, or Vector3.zero Euler):

``````Vector2 currentRotation = Vector2.zero;
Quaternion baseRotation = Quaternion.identity;

// ...

// Combined input for WASD all at once
Vector2 rotationInput = new Vector2(Input.GetAxis("Horizontal") * zRotationSpeed, Input.GetAxis("Vertical") * xRotationSpeed);

// Apply new Input rotation to the running total, then clamp the total
currentRotation += rotationInput;
currentRotation = new Vector2(Mathf.Clamp(currentRotation.z, zRotationCapNeg, zRotationCapPos), Mathf.Clamp(currentRotation.x, xRotationCapNeg, xRotationCapPos));

// I always forget the order of operations on this and I'm not in a position to test right now. The order of multiplications here might need to be sorted.
// (i.e. 6 possible combinations and relative orders of operations for non-commutative multiplication)
transform.rotation = baseRotation * Quaternion.AngleAxis(currentRotation.x, Vector3.forward) * Quaternion.AngleAxis(currentRotation.z, Vector3.right);
``````