- Home /
Gyroscope Magnetic North.
Hi there, I asked a question recently but I believe it was a little confusing and went un answered. I believe I understand the situation a little bit better now and am unsure on how to do it. The gyroscopes attitude is based on the direction of a compass I am guessing So north is north etc. What I need to do I think, is somehow make the gyroscopes attitude relative to the magnetic north (North in relation to the device). So that when I tilt in relation to it, no matter where the Person/Humans rotation is in the world my light will rotate in the right direction. Here's a link to the previous thread and I've posted my code below. Thank you :) http://answers.unity3d.com/questions/1182107/gyroscope-tilting-rotation.html#comment-1182146
private Gyroscope gyro;
public Quaternion rotation = Quaternion.identity;
private Quaternion initialGyroRotation;
private Quaternion initialRotation;
private Quaternion baseRotation;
void Start ()
{
gyro = Input.gyro;
gyro.enabled = true;
initialRotation = transform.rotation;
baseRotation = transform.rotation;
initialGyroRotation = Quaternion.identity;
transform.rotation = gyro.attitude;
Screen.orientation = ScreenOrientation.Portrait;
}
void Update ()
{
#if UNITY_IOS
Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
#endif
#if UNITY_ANDROID
Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, -gyro.attitude.z, -gyro.attitude.w);
#endif
Quaternion offsetRotation = Quaternion.Inverse (initialGyroRotation) * attitudeFix;
rotation = initialRotation * offsetRotation;
transform.rotation = rotation;
}
Answer by elenzil · May 12, 2016 at 06:44 PM
hm, there may be some confusion here.
the gyroscope in devices like a smartphone has nothing to do with north or magnetic fields. the gyroscope reports the angular velocity the device is rotating with. eg, if your device is still, the gyroscope reports 0 deg/s for all 3 axes. if your device is rotating around say the X axis, then the gyroscope's X axis should indicate that.
for getting the device's orientation relative to the earth's magnetic field, you want to access the magnetometer. In unity, this has been nicely packaged up in a class called the Compass.
I guess I got a little confused you see. I'm after just the pitch and roll of the phone. To tilt a light. But lets say I rotated the phone 90 degrees or upside down or walk around my house. The gyroscopes pitch and roll change. I figured there might be a way to make it always relative to the north of the phone to solve the problem.
ah. if you want the pitch and roll of the phone, you may want the Accelerometer.
phones have three motion/orientation detecting components: Accelerometer measures linear acceleration, including gravity. This can be used to deter$$anonymous$$e pitch and roll, but cannot be used to deter$$anonymous$$e yaw. Gyroscope measures angular velocity, which is the change in roll/pitch/yaw over time. but it cannot tell you the absolute roll/pitch/yaw. $$anonymous$$agnetometer measures the phone's orientation relative to the earth's magnetic field.
depending on what you need, you might be able to use just the accelerometer. there are many articles in google about these.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080649im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Fixed tilt rotation on speed up problem 0 Answers
unity horizontal and vertical axis based on gyro? 2 Answers
What is Input.gyro.attitude exactly? 0 Answers
Unity 3d Phone Direction? 0 Answers
Re-calibarting Compass 1 Answer