- Home /

# Rotating object within bounds using gyroscope

I've been playing around with the gyroscope for a bit now and am having some trouble achieving the effect i'm after.

I'm trying to rotate an object using the Gyro but within a limited min max rotation (i.e. -90 to 90 on Y and -20 to 20 on the Z for example).

I've tried using the gyro.attitude but it's information is absolute to the devices orientation. So not super usable for this application.

using gyro.rotationRate gives gyro rotation velocity, which i can then add to the object's localEulerAngle. The problem is when the rotation reaches <0 i get popping and clipping. I realize this is due to eulerAngles not accepting -values.

I've tried getting this to work various ways using localRotation and quaternions but my results are worse off.

```
var lowerLimitY : float = -40;
var upperLimitY : float = 40;
var lowerLimitX : float = -10;
var upperLimitX : float = 10;
var lowerLimitZ : float = -20;
var upperLimitZ : float = 20;
var newRotation : Vector3;
var lastRotation : Vector3;
private var gyroRotRate : Vector3;
function Update () {
if (gyroBool) {
gyroRotRate = gyro.rotationRate;
label.text = "Gyro Rotation Rate = " + gyroRotRate;
//gyro deadzone // sensitivity
if(gyroRotRate.x > 0.1 || gyroRotRate.y > 0.1 || gyroRotRate.z > 0.1)
{
newRotation = lastRotation + gyroRotRate;
}
else if(gyroRotRate.x < -0.1 || gyroRotRate.y < -0.1 || gyroRotRate.z < -0.1)
{
newRotation = lastRotation + gyroRotRate;
}
label3.text = "New Rotation: " + newRotation;
label4.text = "Last Rotation: " + lastRotation;
}
else{label.text = "NO GYRO";
}
}
function LateUpdate()
{
if(newRotation.x > upperLimitX) {transform.eulerAngles.x = upperLimitX; }
else if(newRotation.x < lowerLimitX) {transform.eulerAngles.x = lowerLimitX; }
else{transform.eulerAngles.x = newRotation.x;}
if(newRotation.y > upperLimitY) {transform.eulerAngles.y = upperLimitY; }
else if(newRotation.y < lowerLimitY) {transform.eulerAngles.y = lowerLimitY;}
else{transform.eulerAngles.y = newRotation.y;}
if(newRotation.z > upperLimitZ) {transform.eulerAngles.z = upperLimitZ; }
else if(newRotation.z < lowerLimitZ) {transform.eulerAngles.z = lowerLimitZ; }
else{transform.eulerAngles.z = newRotation.z;}
lastRotation = transform.localEulerAngles;
label2.text = "Object Adjusted EulerAngle = " + transform.eulerAngles;
}
```

Any ideas/suggestions of what i'm doing wrong?

I am facing the same problem. I ain't good at maths but I think the problem should be around the angle rotate will take the shortest path.

http://answers.unity3d.com/questions/29505/jerky-rotation-when-using-quaternionslerp-on-rotat.html

Please let me know if this help or not.

**Answer** by ScroodgeM
·
Jul 27, 2012 at 03:47 PM

after you got gyro.attitude what can't you multiply with device's base rotation to get rotation you need?

then... limiting rotation speed and angle:

## Rotater.cs

using UnityEngine; public class JustForTestCode : MonoBehaviour { public Vector3 Limits = Vector3.one * 50f; public Vector3 Speeds = Vector3.one * 10f; public Transform targetRotation; Vector3 myRotation = Vector3.zero; void Update() { Vector3 targetEuler = targetRotation.rotation.eulerAngles; for (int i = 0; i <= 2; i++) { myRotation[i] = Mathf.Clamp(Mathf.MoveTowards(myRotation[i], Mathf.Repeat(targetEuler[i] + 180f, 360f) - 180f, Time.deltaTime * Speeds[i]), -Limits[i], Limits[i]); } transform.rotation = Quaternion.Euler(myRotation); } }

### 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.