- Home /

# how to clamp y axis with Quaternion.Euler in unity

void Rotate() { Vector3 lastDirectionInGlobal = _camera.ScreenPointToRay(rightFingerLastPoint).direction; Vector3 currentDirectionInGlobal = _camera.ScreenPointToRay(rightFingerCurrentPoint).direction;

```
Quaternion rotation = Quaternion.identity;
rotation.SetFromToRotation(lastDirectionInGlobal, currentDirectionInGlobal);
ownTransform.rotation = ownTransform.rotation * Quaternion.Euler(0, kInverse ? rotation.eulerAngles.y : -rotation.eulerAngles.y, 0);
rotation.SetFromToRotation( cameraTransform.InverseTransformDirection(lastDirectionInGlobal),
cameraTransform.InverseTransformDirection(currentDirectionInGlobal));
cameraTransform.localRotation = Quaternion.Euler(kInverse ? rotation.eulerAngles.x : -rotation.eulerAngles.x, 0, 0) * cameraTransform.localRotation;
rotation = Quaternion.Euler(Mathf.Clamp (rotation.eulerAngles.x, -80, 80),Mathf.Clamp (rotation.eulerAngles.y, -80, 80), 0);//using mathclamp for rotation limit on x & y
rightFingerLastPoint = rightFingerCurrentPoint;
rotation = Quaternion.Euler(cameraTransform.rotation.eulerAngles.x, cameraTransform.rotation.eulerAngles.y, 0);
cameraTransform.rotation = rotation;
}
```

I have done clamping for x axis (-80,80) degree which is working fine, but y axis showing no clamping on (-80,80) degree . Am i doing something wrong. please help me.

You may want to read up on how to format code in questions.

**Answer** by JoeStrout
·
Apr 22, 2015 at 05:33 PM

There is way too much code here. The "rotation" variable seems to be used for about six different things. In particular, you're applying clamping when you assign rotation on line 13, but then on line 17 (before you've used that clamp rotation for anything!), you reassign rotation to something completely different, with no clamping.

So, that's why the clamping doesn't work... I can't make out what your code is intended to do, but if you simplify it and remove the pointless lines (like line 13), I think you'll be able to fix it.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Trouble with Camera Rotation Math 2 Answers

Edited mouselook script rotation clamp not working 0 Answers

Clamp Quaternions Rotation for camera 1 Answer

how do i take gyro rotation and multiply it? 1 Answer

Flip over an object (smooth transition) 3 Answers