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?